aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/verify_value_string_arrays_are_terminated.py
blob: 9f0ad823b8ec3dbfde584fadae059edfce258ae1 (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
#!/usr/bin/env python3
# vim: expandtab tabstop=2 shiftwidth=2 nocin

'''
Usage:
  verify_value_string_arrays_are_terminated.py [ROOT_DIR|PATH] [...]

e.g.
libosmocore/contrib/verify_value_string_arrays_are_terminated.py $(find . -name "*.[hc]")
'''

import re
import sys
import codecs
import os.path

value_string_array_re = re.compile(
  r'((\bstruct\s+value_string\b[^{;]*?)\s*=[^{;]*{[^;]*}\s*;)',
  re.MULTILINE | re.DOTALL)

members = r'(\.(value|str)\s*=\s*)?'
terminator_re = re.compile('{}|{\s*' + members + '(0|NULL)\s*,'
                           '\s*' + members + '(0|NULL)\s*}')
errors_found = 0

def check_file(f):
  global errors_found
  if not (f.endswith('.h') or f.endswith('.c') or f.endswith('.cpp')):
    return
  arrays = value_string_array_re.findall(codecs.open(f, "r", "utf-8").read())
  for array_def, name in arrays:
    if not terminator_re.search(array_def):
      print('ERROR: file contains unterminated value_string %r: %r'
            % (name, f))
      errors_found += 1

args = sys.argv[1:]
if not args:
  args = ['.']

for f in args:
  if os.path.isdir(f):
    for parent_path, subdirs, files in os.walk(f, None, None):
      for ff in files:
        check_file(os.path.join(parent_path, ff))
  else:
        check_file(f)

sys.exit(errors_found)