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/>.
24 import sys, os.path, io, string
28 # error definitions to output
30 ErrorsToCreatDescFor = []
32 # some lookup dictionaries
41 self.err_define = None
45 def escapeString( input ):
46 output = input.replace('"','\\"')
47 output = output.replace("\\<","\\\\<")
48 output = output.replace('\t',"")
51 def transformErrorName( error_name ):
53 if error_name.startswith("STATUS_"):
54 error_name = error_name.replace("STATUS_","",1)
55 elif error_name.startswith("RPC_NT_"):
56 error_name = error_name.replace("RPC_NT_","RPC_",1)
57 elif error_name.startswith("EPT_NT_"):
58 error_name = error_name.replace("EPT_NT_","EPT_",1)
59 new_name = "NT_STATUS_" + error_name
62 def parseErrorDescriptions( file_contents, isWinError ):
64 for line in file_contents:
65 if line == None or line == '\t' or line == "" or line == '\n':
67 content = line.strip().split(None,1)
68 # start new error definition ?
69 if line.startswith("0x"):
71 newError.err_code = int(content[0],0)
72 # escape the usual suspects
74 newError.err_string = escapeString(content[1])
75 newError.linenum = count
76 newError.isWinError = isWinError
77 Errors.append(newError)
80 print "Error parsing file as line %d"%count
83 if err.err_define == None:
84 err.err_define = transformErrorName(content[0])
87 desc = escapeString(line.strip())
89 if err.err_string == "":
92 err.err_string = err.err_string + " " + desc
94 print "parsed %d lines generated %d error definitions"%(count,len(Errors))
96 def parseErrCodeString(error_code_string):
97 # we could develop this more and *really* parse it but realistically
98 # we are only interested in NT_STATUS( mask | code ) or NT_STATUS( code )
99 parts = error_code_string.split('|',1)
103 if len(parts) > 2: #something weird, better warn
104 print "warning something weird unexpected errorcode format ->%s<-"%error_code_string
105 code = int(parts[0],0) | int(parts[1],0)
107 code = int(error_code_string,0)
112 def parseHeaderFile(file_contents):
114 for line in file_contents:
115 contents = line.strip().split(None,2)
116 err_code_string = None
119 if len(contents) > 2:
120 if contents[0] == "#define" and contents[2].startswith("NT_STATUS("):
121 # hairy parsing of lines like
122 # "#define SOMETHING NT_STATUS( num1 | num2 )" etc...
123 err_code_string = contents[2].split('(')[1].split(')')[0]
124 err_code = parseErrCodeString( err_code_string )
126 const_define = contents[1]
127 # print "%s 0x%x"%(const_define, err_code)
128 DefineToErrCode[const_define] = err_code
129 ErrCodeToDefine[err_code] = const_define
131 print "warning: failed to process line[%d] ->%s<-"%(count,line)
133 print "read %d error declarations from header file"%len(ErrCodeToDefine)
135 def generateHeaderFile(out_file):
136 out_file.write("\n\n")
137 out_file.write("/*\n")
138 out_file.write(" * New descriptions for new errors generated from\n")
139 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
140 out_file.write(" */\n\n")
141 for err in ErrorsToUse:
142 line = "#define {0:49} NT_STATUS(0x{1:08X})\n".format(err.err_define ,err.err_code)
146 def generateSourceFile(out_file):
147 out_file.write("/*\n")
148 out_file.write(" * New descriptions for existing errors generated from\n")
149 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
150 out_file.write(" */\n")
151 for err in ErrorsToCreatDescFor:
152 out_file.write(" { N_(\"%s\"), %s },\n"%(err.err_string, err.err_define))
153 out_file.write("\n\n")
154 out_file.write("/*\n")
155 out_file.write(" * New descriptions for new errors generated from\n")
156 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
157 out_file.write(" */\n")
158 for err in ErrorsToUse:
159 out_file.write(" { N_(\"%s\"), %s },\n"%(err.err_string, err.err_define))
160 out_file.write("\n\n");
161 out_file.write("/*\n")
162 out_file.write(" * New descriptions for new errors generated from\n")
163 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
164 out_file.write(" */\n")
165 for err in ErrorsToUse:
166 out_file.write(" { \"%s\", %s },\n"%(err.err_define, err.err_define))
168 def def_in_list(define, err_def_with_desc):
169 for item in err_def_with_desc:
170 if item.strip() == define:
174 def processErrorDescription(err_def_with_desc):
175 print "processing error descriptions...."
178 # do we have an error with this error code already ?
179 if ErrCodeToDefine.has_key(err.err_code):
180 already_has_desc = def_in_list(ErrCodeToDefine[err.err_code], err_def_with_desc)
181 # no 'full' error description for this error code so create a new
183 if already_has_desc == False:
184 # synthesise a new Error object to create desc from
185 new_error = ErrorDef()
186 new_error.err_define = ErrCodeToDefine[err.err_code]
187 new_error.err_code = err.err_code
188 new_error.err_string = err.err_string
189 new_error.linenum = err.linenum
190 ErrorsToCreatDescFor.append(new_error)
193 ErrorsToUse.append(err)
195 print "skipped %d existing definitions"%count
196 print "imported %d new error definitions"%(len(ErrorsToUse))
197 print "created %d new error descriptions for existing errors"%(len(ErrorsToCreatDescFor))
199 # Very simple script to generate files ntstatus.c & ntstatus.h, these
200 # files contain generated content used to add to the existing content
201 # of files nterr.c & ntstatus.h.
202 # The script takes 3 inputs
203 # 1. location of the existing ntstatus.h (which is used to build a list of
204 # existing error defines
205 # 2. a text file, format which is very simple and is just the content of a
206 # html table ( such as that found in
207 # http://msdn.microsoft.com/en-us/library/cc231200.aspx ) copied and
208 # pasted into a text file
209 # 3. finally a text file containing names of the error defines (1 per line)
210 # that already are in ntstatus.h/nterr.c but that only have the 'short'
211 # error description ( short error description is where the description is
212 # the name of the error itself e.g. "NT_STATUS_SUCCESS" etc.
217 filename = "ntstatus"
218 headerfile_name = filename + ".h"
219 sourcefile_name = filename + ".c"
220 if len(sys.argv) > 3:
221 input_file1 = sys.argv[1]
222 input_file2 = sys.argv[2]
223 input_file3 = sys.argv[3]
225 print "usage: %s headerfile winerrorfile existing_short_descs"%(sys.argv[0])
229 file_contents = open(input_file1,"r")
230 parseHeaderFile(file_contents)
231 file_contents = open(input_file2,"r")
232 parseErrorDescriptions(file_contents, False)
233 file_contents = open(input_file3,"r")
234 has_already_desc = file_contents.readlines()
235 processErrorDescription(has_already_desc)
236 print "writing new headerfile: %s"%headerfile_name
237 out_file = open(headerfile_name,"w")
238 generateHeaderFile(out_file)
240 print "writing new headerfile: %s"%sourcefile_name
241 out_file = open(sourcefile_name,"w")
242 generateSourceFile(out_file)
245 if __name__ == '__main__':