blob: 3d23ef02ec2fe91eb2a2acbb2bebb7cb4711d09d [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/env python2.5
2"""This script reads C/C++ header file and output query
3
4Author ykk
5Date June 2009
6"""
7import sys
8import getopt
9import cheader
10
11def 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
28try:
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"])
33except getopt.GetoptError:
34 usage()
35 sys.exit(2)
36
37#Check there is at least input file
38if (len(args) < 1):
39 usage()
40 sys.exit(2)
41
42#Parse options
43##Print names only
44nameOnly = False
45##Print all structs?
46allStructs = False
47##Query specific struct
48struct=""
49##Print all enums?
50allEnums = False
51##Query specific enum
52enum=""
53##Print all macros?
54allMacros = False
55##Query specific macro
56macro=""
57##Print without comment
58printNoComment=False
59for 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
82headerfile = cheader.cheaderfile(args)
83if (printNoComment):
84 for line in headerfile.content:
85 print line
86 sys.exit(0)
87
88#Print all macros
89if (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
96if (macro != ""):
97 try:
98 print macro+"="+headerfile.macros[macro]
99 except KeyError:
100 print "Macro "+macro+" not found!"
101
102#Print all structs
103if (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
111if (struct != ""):
112 try:
113 print str(headerfile.structs[struct])
114 except KeyError:
115 print "Struct "+struct+" not found!"
116
117#Print all enumerations
118if (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
130if (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!"