Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python2.5 |
| 2 | """This script reads C/C++ header file and output query |
| 3 | |
| 4 | Author ykk |
| 5 | Date June 2009 |
| 6 | """ |
| 7 | import sys |
| 8 | import getopt |
| 9 | import cheader |
| 10 | |
| 11 | def usage(): |
| 12 | """Display usage |
| 13 | """ |
| 14 | print "Usage "+sys.argv[0]+" <options> header_file_1 header_file_2 ...\n"+\ |
| 15 | "Options:\n"+\ |
| 16 | "-h/--help\n\tPrint this usage guide\n"+\ |
| 17 | "-E/--enums\n\tPrint all enumerations\n"+\ |
| 18 | "-e/--enum\n\tPrint specified enumeration\n"+\ |
| 19 | "-M/--macros\n\tPrint all macros\n"+\ |
| 20 | "-m/--macro\n\tPrint value of macro\n"+\ |
| 21 | "-S/--structs\n\tPrint all structs\n"+\ |
| 22 | "-s/--struct\n\tPrint struct\n"+\ |
| 23 | "-n/--name-only\n\tPrint names only\n"+\ |
| 24 | "-P/--print-no-comment\n\tPrint with comment removed only\n"+\ |
| 25 | "" |
| 26 | |
| 27 | #Parse options and arguments |
| 28 | try: |
| 29 | opts, args = getopt.getopt(sys.argv[1:], "hMm:Ee:Ss:nP", |
| 30 | ["help","macros","macro=","enums","enum=", |
| 31 | "structs","struct=" |
| 32 | "name-only","print-no-comment"]) |
| 33 | except getopt.GetoptError: |
| 34 | usage() |
| 35 | sys.exit(2) |
| 36 | |
| 37 | #Check there is at least input file |
| 38 | if (len(args) < 1): |
| 39 | usage() |
| 40 | sys.exit(2) |
| 41 | |
| 42 | #Parse options |
| 43 | ##Print names only |
| 44 | nameOnly = False |
| 45 | ##Print all structs? |
| 46 | allStructs = False |
| 47 | ##Query specific struct |
| 48 | struct="" |
| 49 | ##Print all enums? |
| 50 | allEnums = False |
| 51 | ##Query specific enum |
| 52 | enum="" |
| 53 | ##Print all macros? |
| 54 | allMacros = False |
| 55 | ##Query specific macro |
| 56 | macro="" |
| 57 | ##Print without comment |
| 58 | printNoComment=False |
| 59 | for opt,arg in opts: |
| 60 | if (opt in ("-h","--help")): |
| 61 | usage() |
| 62 | sys.exit(0) |
| 63 | elif (opt in ("-S","--structs")): |
| 64 | allStructs = True |
| 65 | elif (opt in ("-s","--struct")): |
| 66 | struct = arg |
| 67 | elif (opt in ("-M","--macros")): |
| 68 | allMacros = True |
| 69 | elif (opt in ("-m","--macro")): |
| 70 | macro=arg |
| 71 | elif (opt in ("-E","--enums")): |
| 72 | allEnums = True |
| 73 | elif (opt in ("-e","--enum")): |
| 74 | enum = arg |
| 75 | elif (opt in ("-n","--name-only")): |
| 76 | nameOnly = True |
| 77 | elif (opt in ("-P","--print-no-comment")): |
| 78 | printNoComment = True |
| 79 | else: |
| 80 | assert (False,"Unhandled option :"+opt) |
| 81 | |
| 82 | headerfile = cheader.cheaderfile(args) |
| 83 | if (printNoComment): |
| 84 | for line in headerfile.content: |
| 85 | print line |
| 86 | sys.exit(0) |
| 87 | |
| 88 | #Print all macros |
| 89 | if (allMacros): |
| 90 | for (macroname, value) in headerfile.macros.items(): |
| 91 | if (nameOnly): |
| 92 | print macroname |
| 93 | else: |
| 94 | print macroname+"\t=\t"+str(value) |
| 95 | #Print specified macro |
| 96 | if (macro != ""): |
| 97 | try: |
| 98 | print macro+"="+headerfile.macros[macro] |
| 99 | except KeyError: |
| 100 | print "Macro "+macro+" not found!" |
| 101 | |
| 102 | #Print all structs |
| 103 | if (allStructs): |
| 104 | for (structname, value) in headerfile.structs.items(): |
| 105 | if (nameOnly): |
| 106 | print structname |
| 107 | else: |
| 108 | print str(value)+"\n" |
| 109 | |
| 110 | #Print specified struct |
| 111 | if (struct != ""): |
| 112 | try: |
| 113 | print str(headerfile.structs[struct]) |
| 114 | except KeyError: |
| 115 | print "Struct "+struct+" not found!" |
| 116 | |
| 117 | #Print all enumerations |
| 118 | if (allEnums): |
| 119 | for (enumname, values) in headerfile.enums.items(): |
| 120 | print enumname |
| 121 | if (not nameOnly): |
| 122 | for enumval in values: |
| 123 | try: |
| 124 | print "\t"+enumval+"="+\ |
| 125 | str(headerfile.enum_values[enumval]) |
| 126 | except KeyError: |
| 127 | print enumval+" not found in enum!"; |
| 128 | |
| 129 | #Print specifed enum |
| 130 | if (enum != ""): |
| 131 | try: |
| 132 | for enumval in headerfile.enums[enum]: |
| 133 | try: |
| 134 | print enumval+"="+str(headerfile.enum_values[enumval]) |
| 135 | except KeyError: |
| 136 | print enumval+" not found in enum!"; |
| 137 | except KeyError: |
| 138 | print "Enumeration "+enum+" not found!" |