# This program takes a C header/source as the input and produces # with --keyword=enum: the list of all enums # with --keyword=struct: the list of all structs # --enum DBUS_POINTER_NAME1, from __future__ import absolute_import, division, print_function def toprint(match, line): return args.pattern % match parser = argparse.ArgumentParser() parser.add_argument('input', nargs='*', help='Input files (or stdin if not specified)') parser.add_argument('-o', '--output', type=argparse.FileType('w'), help='Output to file instead of stdout') parser.add_argument('--keyword', default='struct', help='What keyword to search') parser.add_argument('--pattern', default='%s', help='String pattern used to print matches') parser.add_argument('--verbatim', action='store_true', help='Return full line of match instead of match itself') args = parser.parse_args() structregexp1 = re.compile( r"^(typedef\s+)?%s\s+\w+\s+(\w+)\s*;" % (args.keyword, )) structregexp2 = re.compile(r"^(typedef\s+)?%s" % (args.keyword, )) structregexp3 = re.compile(r"^}\s+(\w+)\s*;") print("/* Generated by %s. Do not edit! */" % (sys.argv[0], ), myinput = fileinput.input(args.input) match = structregexp1.match(line) print(toprint(match.group(2), line), file=args.output) match = structregexp2.match(line) print(line.rstrip(), file=args.output) match = structregexp3.match(line) print(toprint(match.group(1), line), file=args.output) if line[0] not in " \t{\n": print(line, file=args.output)