summaryrefslogtreecommitdiffstats
path: root/source4/scripting/bin/gen_error_common.py
blob: 390d1d701b611fdaba4e433933ecd75808da54c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3

#
# Unix SMB/CIFS implementation.
#
# Utility methods for generating error codes from a file.
#
# Copyright (C) Noel Power <noel.power@suse.com> 2014
# Copyright (C) Catalyst IT Ltd. 2017
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# error data model
class ErrorDef:
    def __init__(self):
        self.err_code = None
        self.err_define = None
        self.err_string = ""
        self.isWinError = False
        self.linenum = None

def escapeString( input ):
    output = input.replace('"','\\"')
    output = output.replace("\\<","\\\\<")
    output = output.replace('\t',"")
    return output

# Parse error descriptions from a file which is the content
# of an HTML table.
# The file must be formatted as:
# [error code hex]
# [error name short]
# [error description]
# Blank lines are allowed and errors do not have to have a
# description.
# Returns a list of ErrorDef objects.
def parseErrorDescriptions( file_contents, isWinError, transformErrorFunction ):
    errors = []
    count = 0
    for line in file_contents:
        if line is None or line == '\t' or line == "" or line == '\n':
            continue
        content = line.strip().split(None,1)
        # start new error definition ?
        if len(content) == 0:
            continue
        if line.startswith("0x"):
            newError = ErrorDef()
            newError.err_code = int(content[0],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:
                continue
            err = errors[-1]
            if err.err_define is None:
                err.err_define = transformErrorFunction(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
    print("parsed %d lines generated %d error definitions"%(count,len(errors)))
    return errors