TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / tools / convert-proto-init.py
blobf0ce652ff84a9cf516ad47acad5d131a98883165
1 #!/usr/bin/env python3
3 # Wireshark - Network traffic analyzer
4 # By Gerald Combs <gerald@wireshark.org>
5 # Copyright 1998 Gerald Combs
7 # SPDX-License-Identifier: GPL-2.0-or-later
8 '''\
9 convert-proto-init.py - Remove explicit init of proto variables.
10 '''
12 # Imports
14 import argparse
15 import glob
16 import platform
17 import re
18 import sys
20 def convert_file(file):
21 lines = ''
22 try:
23 with open(file, 'r') as f:
24 lines = f.read()
25 # Match the following proto, header field, expert info and subtree variables:
27 # static int proto_a = -1;
28 # int proto_b=-1;
30 # static int hf_proto_a_value_1 = -1;
31 # int hf_proto_a_value_2 = - 1;
32 # int hf_proto_a_value_3=-1;
33 # /* static int hf_proto_a_unused_1 = -1; */
35 # static gint ett_proto_a_tree_1=-1;
36 # gint ett_proto_a_tree_2 = -1; /* A comment. */
38 # static expert_field ei_proto_a_expert_1 = EI_INIT;
40 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)
41 except IsADirectoryError:
42 sys.stderr.write(f'{file} is a directory.\n')
43 return
44 except UnicodeDecodeError:
45 sys.stderr.write(f"{file} isn't valid UTF-8.\n")
46 return
47 except Exception:
48 sys.stderr.write(f'Unable to open {file}.\n')
49 return
51 with open(file, 'w') as f:
52 f.write(lines)
53 print(f'Converted {file}')
55 def main():
56 parser = argparse.ArgumentParser(description='Initialize static proto values to 0.')
57 parser.add_argument('files', metavar='FILE', nargs='*')
58 args = parser.parse_args()
60 files = []
61 if platform.system() == 'Windows':
62 for arg in args.files:
63 files += glob.glob(arg)
64 else:
65 files = args.files
67 for file in files:
68 convert_file(file)
70 # On with the show
72 if __name__ == "__main__":
73 sys.exit(main())