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 ?
53 if line
.startswith("0x"):
55 newError
.err_code
= content
[0]
56 # escape the usual suspects
58 newError
.err_string
= escapeString(content
[1])
59 newError
.linenum
= count
60 newError
.isWinError
= isWinError
61 Errors
.append(newError
)
64 print "Error parsing file as line %d"%count
67 if err
.err_define
== None:
68 err
.err_define
= "HRES_" + content
[0]
71 desc
= escapeString(line
.strip())
73 if err
.err_string
== "":
76 err
.err_string
= err
.err_string
+ " " + desc
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")
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"
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")
130 line
= "#define {0:49} HRES_ERROR({1})\n".format(err
.err_define
,err
.err_code
)
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")
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")
154 out_file
.write(" {\n")
156 out_file
.write(" HRESULT_FROM_WIN32(%s),\n"%err.err_define
)
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")
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
190 headerfile_name
= filename
+ ".h"
191 sourcefile_name
= filename
+ ".c"
192 if len(sys
.argv
) > 1:
193 input_file1
= sys
.argv
[1]
195 print "usage: %s winerrorfile"%(sys
.argv
[0])
198 parseErrorDescriptions(input_file1
, False)
199 out_file
= open(headerfile_name
,"w")
200 generateHeaderFile(out_file
)
202 out_file
= open(sourcefile_name
,"w")
203 generateSourceFile(out_file
)
205 if __name__
== '__main__':