annotate make_headers.py @ 8:ae45108716a2

fix usage
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 14 Sep 2015 13:13:17 +0900
parents d66cc78ae824
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #!/usr/bin/env python3.0
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import sys
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 import re
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 import getopt
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 reserved_words = [ "if", "for", "switch", "return", "while", "else", ]
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 PATTERN = "([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^{/;]*)\)\s*\{"
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 # TODO: 関数パラメータ内にコメントがあると正しく動かない!
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 # TODO: int * const * とか大丈夫?
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 PROG = re.compile(PATTERN, re.S)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 omit_static=False
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 add_extern=""
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 def truncate_comments(data):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 pass
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 def check_reserved_word(decl):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 """ return true if decl's type and name is not reserved word. """
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 if decl["name"] in reserved_words or decl["type"] in reserved_words:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 return False
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 return True
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 def read_decls(file):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 declarators = []
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 # open the file and read all lines into a string.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 try:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 fo = open(file, 'r')
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 lines = fo.readlines()
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 data = "".join(lines)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 truncate_comments(data)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 except IOError:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 print("cannot read file %s" % file)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return None
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 # find all matched strings.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 # moiter is iterator of MatchObject.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 moiter = PROG.finditer(data)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 for mo in moiter:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 tmp = { "type": mo.group(1),
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 "name": mo.group(2),
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 "parms": mo.group(3),
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 "offset": mo.start() }
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 if check_reserved_word(tmp):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 declarators.append(tmp)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 return declarators
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 def debug_print(decl):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 for (key,value) in list(decl.items()):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 if isinstance(value, str):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 decl[key] = value.replace("\n"," ").replace("\t"," ")
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 print("Type:\t{0:s}".format(decl["type"]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 print("Name:\t{0:s}".format(decl["name"]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 print("Params:\t{0:s}".format(decl["parms"]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 print("offset:\t{0:d}".format(decl["offset"]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 print("")
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 def format_print(decl, file):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 for (key,value) in list(decl.items()):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 if isinstance(value, str):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 decl[key] = value.replace("\n"," ").replace("\t"," ")
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 print("/* defined in file {0:s} at offset {1:d} */".format(file,decl["offset"]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 print("{3:s}{0:s} {1:s} ({2:s});".format(decl["type"],decl["name"],decl["parms"], add_extern))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 print("")
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 def getoptions():
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 global omit_static, add_extern
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 try:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 opts, args = getopt.getopt(sys.argv[1:], 'se', [ 'omit-static', 'add-extern' ])
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 except getopt.GetoptError as err:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 print(err)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 usage()
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 sys.exit(2)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 for opt,a in opts:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 if opt in ("-s", "--omit-static"):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 omit_static=True
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 elif opt in ("-e", "--add-extern"):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 add_extern="extern "
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 else:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 print("unhandled option {0}".format(opt))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 usage()
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 return args
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 def usage():
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 print( """\
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 Usage: {0:s} OPION... [FILE]...
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 OPTIONS:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 -s, --omit-static omit static functions
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 -e, --add-extern add extern to all function declarations
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 """.format(sys.argv[0]))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 def main():
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 # option handling.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 args = getoptions()
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 for file in args:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 # read function declaration from each file.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 decls = read_decls(file)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 if decls==None or len(decls)==0:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 # no function found.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 print("{0} have no function definition!".format(file))
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 continue
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 for decl in decls:
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 if omit_static and 0 <= decl["type"].find("static"):
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 # static function is ignored.
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 continue
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 #debug_print(decl)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 format_print(decl, file)
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 main()
d66cc78ae824 initializing and add make headers script
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124