def create_ast(): """Parse a C headerfile with C preprocessor, and create an AST.""" cpp_h = os.path.join(os.path.dirname(__file__), 'cpp.h') inc_h = os.path.join(os.path.dirname(__file__), 'include.h') assert os.path.isfile(cpp_h) and os.path.isfile(inc_h) text = cpp.parse_file(cpp_h) yield cparser.parse(text)
def create_dissector(filename, platform, folders=None, includes=None): """Parse 'filename' to create a Wireshark protocol dissector. 'filename' is the C header/code file to parse. 'platform' is the platform we should simulate. 'folders' is a set of all folders to -Include. 'includes' is a set of filenames to #include. Returns the error if parsing failed, None if succeeded. """ try: text = cpp.parse_file(filename, platform, folders, includes) ast = cparser.parse(text, filename) cparser.find_structs(ast, platform) except OSError: raise except Exception as err: # Clean up a bit try: del cparser.StructVisitor._last_proto.dissectors[platform] del cparser.StructVisitor._last_visitor del cparser.StructVisitor._last_diss del cparser.StructVisitor._last_proto except Exception: pass if Options.verbose: print('Failed "%s":%s which raised %s' % (filename, platform.name, repr(err))) if Options.debug: sys.excepthook(*sys.exc_info()) return err if Options.verbose: print("Parsed header file '%s':%s successfully." % (filename, platform.name))
def create_ast(): """Parse a C headerfile with C preprocessor, and create an AST.""" # Find the header files and cpp if we are run from a different folder cpp_h = os.path.join(os.path.dirname(__file__), 'cpp.h') inc_h = os.path.join(os.path.dirname(__file__), 'include.h') assert os.path.isfile(cpp_h) and os.path.isfile(inc_h) text = cpp.parse_file(cpp_h) yield cparser.parse(text)