forked from psas/elderberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader2Miml.py
executable file
·158 lines (135 loc) · 4.73 KB
/
header2Miml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python
# Assumptions:
# - only parses extern functions
# - init/final functions only detected with "initialize" and "finalize" in name
# - any non-init/final functions with "initialize" or "finalize" aren't included
# - only data types recognized are [const|unsigned] [int|char]
# - variable names not defined are replaced by ARG# variable name
import sys, re
if (len(sys.argv) < 2):
print "Error: Too few arguments."
sys.exit(-1)
elif len(sys.argv) > 3:
print "Error: Too many arguments."
sys.exit(-1)
inputfile = sys.argv[1]
rpos = inputfile.rfind(".")
if len(sys.argv) == 2:
outputfile = inputfile
outputfile += ".miml"
if str(inputfile[rpos:]) != ".h" and (rpos > 0):
print "Error: Input file not a .h file."
sys.exit(-1)
elif rpos < 0:
objfile = inputfile + ".o"
inputfile += ".h"
else:
objfile = inputfile[:rpos] + ".o"
if len(sys.argv) == 3:
outputfile = sys.argv[2]
rpos = outputfile.rfind(".");
if (str(outputfile[rpos:]) != ".miml") and (rpos > 0):
print "Error: Output file not a .miml file."
sys.exit(-1)
elif rpos < 0:
outputfile += ".miml"
try:
f = open(inputfile, 'r')
except IOError as e:
print "I/O error({0}): Input header file --> {1}".format(e.errno, e.strerror)
sys.exit(-1)
codeLines = f.readlines()
f.close()
try:
f = open('cg.conf', 'r')
except IOError as e:
print "I/O error({0}): cg.conf --> {1}".format(e.errno, e.strerror)
sys.exit(-1)
cgConf = f.readlines()
f.close()
for item in cgConf:
match = re.match( r'[\s]*allowed_types: \[(.*)\][\s]*', item, re.M|re.I)
if match:
allowedTypes = match.group(1)
allowedTypes = re.sub( r"\,[\s]*", "|", allowedTypes)
allowedTypes = re.sub( r"'", "", allowedTypes)
allowedTypes = re.sub( r"(const|unsigned)[\s]+", "", allowedTypes)
break
outputCodeHeader = "%YAML 1.2\n---\ninclude: " + inputfile + "\nobject: " + objfile
outputCodeInit = "init: "
outputCodeFinal = "final: "
outputCodeSenders = "# Functions that handle outgoing data\nsenders:\n"
outputCodeReceivers = "# Functions that handle incoming data\nreceivers:\n"
outputCodeUnknown = "[unknown:]\n"
foundInit = 0
foundFinal = 0
foundUnknown = 0
foundSenders = 0
foundReceivers = 0
def xstr(s):
if s is None:
return ''
return str(s)
for item in codeLines:
#match = re.match( r'[\s]*(extern[\s]+)?([\w]+[\s]+)([\w_-]+)[\s]*\((.*)\).*', item.strip('\n'), re.M|re.I)
match = re.match( r'[\s]*(extern[\s]+)?([\w]+[\s]+)([\w_-]+)[\s]*\((.*)\)[\s]*;[\s]*(\/\/[\s]*\[[\s]*miml[\s]*:[\s]*(init|final|sender|receiver)[\s]*\])?.*', item.strip('\n'), re.M|re.I)
if match:
mimlType = match.group(6)
if(mimlType=="init"):
outputCodeInit += str(match.group(3)) + "();"
foundInit += 1
continue
if(mimlType=="final"):
outputCodeFinal += str(match.group(3)) + "();"
foundFinal += 1
continue
content = match.group(4).split(',')
counter = 0
argVals = ""
for item2 in content:
counter += 1
match2 = re.match( r'[\s]*((const|unsigned)[\s]+)?(' + allowedTypes + ')[\s]*(\*)?([\s]*([\w_-]+)[\s]*)?', item2, re.M|re.I)
if match2:
argType = xstr(match2.group(1)) + xstr(match2.group(3)) + xstr(match2.group(4))
argName = match2.group(6)
if argName is None:
argName = "ARG" + str(counter)
argVals += " - [" +argName + ", " + argType.strip() + "]\n"
funcName = str(match.group(3))
if (mimlType=="receiver"):
outputCodeReceivers += " " + funcName + ":\n" + argVals
foundReceivers += 1
elif (mimlType=="sender"):
outputCodeSenders += " " + funcName + ":\n" + argVals
foundSenders += 1
else:
outputCodeUnknown += " " + funcName + ":\n" + argVals
foundUnknown += 1
try:
fout = open(outputfile, 'w')
except IOError as e:
print "I/O error({0}): Output Miml file --> {1}".format(e.errno, e.strerror)
sys.exit(-1)
fout.write(outputCodeHeader + "\n")
if(foundInit):
fout.write(outputCodeInit + "\n")
if(foundFinal):
fout.write(outputCodeFinal + "\n\n")
else:
fout.write("\n")
fout.write(outputCodeSenders + "\n")
fout.write(outputCodeReceivers + "\n")
#if foundUnknown:
# fout.write("# Functions that have not been designated as\n")
# fout.write("# senders or receivers. Sort them accordingly\n")
# fout.write("# and delete the [unknown:] header.\n")
# fout.write(outputCodeUnknown + "\n")
fout.close()
print "\n " + inputfile + " -> " + outputfile
print "======================================================"
#print " Init\tFinal\tSenders\t Receivers Unknown "
print " Init\tFinal\tSenders\t Receivers "
print "------------------------------------------------------"
#print " " + str(foundInit) + "\t" + str(foundFinal) + "\t" + str(foundSenders) + "\t " + str(foundReceivers) + "\t " + str(foundUnknown)
print " " + str(foundInit) + "\t" + str(foundFinal) + "\t" + str(foundSenders) + "\t " + str(foundReceivers)
print ""