summaryrefslogtreecommitdiffstats
path: root/tools/convert-proto-init.py
blob: f0ce652ff84a9cf516ad47acad5d131a98883165 (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
#!/usr/bin/env python3
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
'''\
convert-proto-init.py - Remove explicit init of proto variables.
'''

# Imports

import argparse
import glob
import platform
import re
import sys

def convert_file(file):
    lines = ''
    try:
        with open(file, 'r') as f:
            lines = f.read()
            # Match the following proto, header field, expert info and subtree variables:
            #
            # static int proto_a = -1;
            # int proto_b=-1;
            #
            # static int hf_proto_a_value_1     = -1;
            #        int hf_proto_a_value_2     = - 1;
            # int hf_proto_a_value_3=-1;
            # /* static int hf_proto_a_unused_1   = -1; */
            #
            # static gint ett_proto_a_tree_1=-1;
            # gint ett_proto_a_tree_2 = -1; /* A comment. */
            #
            # static expert_field ei_proto_a_expert_1 = EI_INIT;
            #
            lines = re.sub(r'^((?://\s*|/[*]+\s*)?(?:static\s*|       )?(?:g?int|expert_field)\s*(?:proto|hf|ett|ei)_[\w_]+)\s*=\s*(?:-\s*1|EI_INIT)\s*', r'\1', lines, flags=re.MULTILINE)
    except IsADirectoryError:
        sys.stderr.write(f'{file} is a directory.\n')
        return
    except UnicodeDecodeError:
        sys.stderr.write(f"{file} isn't valid UTF-8.\n")
        return
    except Exception:
        sys.stderr.write(f'Unable to open {file}.\n')
        return

    with open(file, 'w') as f:
        f.write(lines)
    print(f'Converted {file}')

def main():
    parser = argparse.ArgumentParser(description='Initialize static proto values to 0.')
    parser.add_argument('files', metavar='FILE', nargs='*')
    args = parser.parse_args()

    files = []
    if platform.system() == 'Windows':
        for arg in args.files:
            files += glob.glob(arg)
    else:
        files = args.files

    for file in files:
        convert_file(file)

# On with the show

if __name__ == "__main__":
    sys.exit(main())