]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s4:scripting: Use common function to parse error descriptions
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Wed, 10 Jan 2024 22:19:22 +0000 (11:19 +1300)
committerJoseph Sutton <jsutton@samba.org>
Mon, 15 Jan 2024 00:48:40 +0000 (00:48 +0000)
The version of parseErrorDescriptions() from gen_error_common is almost
the same as the one we’ve been using. One minor difference is that
ErrorDef.error_code is now an integer rather than a string.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
source4/scripting/bin/gen_hresult.py

index 490f40488e8ba6eed1bdbb21df0b8d5503e0843d..8e25dad0c98f31507b8c50d856bc14199248208b 100755 (executable)
 
 
 import sys
+from gen_error_common import parseErrorDescriptions
 
 # parsed error data
 Errors = []
 
-# error data model
-class ErrorDef:
-
-    def __init__(self):
-        self.err_code = ""
-        self.err_define = None
-        self.err_string = ""
-        self.isWinError = False
-        self.linenum = ""
-
-def escapeString( input ):
-    output = input.replace('"','\\"')
-    output = output.replace("\\<","\\\\<")
-    output = output.replace('\t',"")
-    return output
-
-def parseErrorDescriptions( input_file, isWinError ):
-    # read in the data
-    fileContents = open(input_file,"r")
-    count = 0
-    for line in fileContents:
-        content = line.strip().split(None,1)
-        # start new error definition ?
-        if len(content) == 0:
-            continue
-        if line.startswith("0x"):
-            newError = ErrorDef()
-            newError.err_code = content[0]
-            # escape the usual suspects
-            if len(content) > 1:
-                newError.err_string = escapeString(content[1])
-            newError.linenum = count
-            newError.isWinError = isWinError
-            Errors.append(newError)
-        else:
-            if len(Errors) == 0:
-                print("Error parsing file as line %d"%count)
-                sys.exit()
-            err = Errors[-1]
-            if err.err_define is None:
-                err.err_define = "HRES_" + content[0]
-            else:
-                if len(content) > 0:
-                    desc =  escapeString(line.strip())
-                    if len(desc):
-                        if err.err_string == "":
-                            err.err_string = desc
-                        else:
-                            err.err_string = err.err_string + " " + desc
-        count = count + 1
-    fileContents.close()
-    print("parsed %d lines generated %d error definitions"%(count,len(Errors)))
-
 def write_license(out_file):
     out_file.write("/*\n")
     out_file.write(" * Unix SMB/CIFS implementation.\n")
@@ -129,7 +77,7 @@ def generateHeaderFile(out_file):
     out_file.write("\n")
 
     for err in Errors:
-        line = "#define {0:49} HRES_ERROR({1})\n".format(err.err_define ,err.err_code)
+        line = "#define {0:49} HRES_ERROR(0x{1:08X})\n".format(err.err_define ,err.err_code)
         out_file.write(line)
     out_file.write("\nconst char *hresult_errstr_const(HRESULT err_code);\n")
     out_file.write("\nconst char *hresult_errstr(HRESULT err_code);\n")
@@ -199,6 +147,9 @@ def generateSourceFile(out_file):
     out_file.write("   return msg;\n")
     out_file.write("};\n")
 
+def transformErrorName(error_name):
+    return "HRES_" + error_name
+
 # Very simple script to generate files hresult.c & hresult.h
 # The script simply takes a text file as input, format of input file is
 # very simple and is just the content of a html table ( such as that found
@@ -216,7 +167,11 @@ def main ():
         print("usage: %s winerrorfile"%(sys.argv[0]))
         sys.exit()
 
-    parseErrorDescriptions(input_file1, False)
+    # read in the data
+    with open(input_file1) as file_contents:
+        global Errors
+        Errors = parseErrorDescriptions(file_contents, False, transformErrorName)
+
     out_file = open(headerfile_name,"w")
     generateHeaderFile(out_file)
     out_file.close()