-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.py
345 lines (299 loc) · 8.37 KB
/
funcs.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/python
import re
import sys
import getopt
#
# find C-style functions
#
def find_c_function_declaration(line):
candidate = re.compile(r'([a-zA-Z0-9]+)\(([a-zA-Z0-9\*\s,&]+)\)(;)?(\{)?')
matches = candidate.search(line)
if matches:
func_name = matches.group(1)
func_args = matches.group(2)
func_term = matches.group(3)
if func_term:
func_description = (func_name, func_args, 'declaration')
else:
func_description = (func_name, func_args, 'definition' )
return func_description
else:
return None
#
# Is the trailing non-white space character a open bracket...
#
def is_trailing_char_bracket(line):
for c in line[::-1]:
if c == ' ' or c == '\n':
continue
else:
# the actual test
if c == '{':
return True
else:
return False
return False
#
# Is the leading non-white space character an open bracket
#
def is_leading_char_bracket(line):
for c in line:
if c == ' ' or c == '\t':
continue
else:
# the actual test
if c == '{':
return True
else:
return False
#
# usage function
#
def print_usage():
pname = sys.argv[0]
print "USAGE: %s <infile> <optional-outfile>" % (pname)
#
# insert C-style macro definition
#
def write_macro(macro_name):
macro_top = "#define %s(fmt, ...) \\\n" % (macro_name)
macro_bot = "\tdo { fprintf(stdout,\"%s:%d:%s(): \" fmt \"\\n\",__FILE__,__LINE__,__func__,__VA_ARGS__); } while(0)\n"
macro = macro_top + macro_bot
return macro
#
# insert macro call
#
def write_macro_call(macro_name):
if type(macro_name) != type(''):
print "ERROR macro name is not a string: ",macro_name
macrostr = macro_name + '("","");\n'
return macrostr
#
# find_macro_candidate
#
def find_macro_candidate(fhandle):
# macro definition candidates...
candidates = ['DPRINTF','DTELL']
fhandle.seek(0)
fstring = fhandle.read()
fhandle.seek(0)
for candidate in candidates:
needle = re.compile(candidate)
if needle.search(fstring):
continue
else:
return candidate
return None
#
# Is using an existing macro
#
def is_using_macro(fhandle):
# macro definition candidates...
candidates = ['DPRINTF','DTELL']
fhandle.seek(0)
fstring = fhandle.read()
fhandle.seek(0)
for candidate in candidates:
needle = re.compile(candidate)
if needle.search(fstring):
return candidate
else:
continue
return None
#
# Find the end of the header
#
def find_header(fhandle):
fhandle.seek(0)
iline = 0
last_line = 0
blank_line = 0
increment_blank = False
increg = re.compile(r'(#import)|(#include)')
for line in fhandle:
iline = iline + 1
if increg.findall(line):
last_line = iline
increment_blank = True
if increment_blank and len(line) <= 1:
blank_line = iline
increment_blank= False
# done finding the header
fhandle.seek(0)
# increment to be sure
last_line = blank_line
return last_line
#
# remove lines with the expression
#
def remove_lines_with_expression(infhandle,exp):
rexp = re.compile(exp)
infhandle.seek(0)
lines = []
for line in infhandle.readlines():
if rexp.search(line):
continue
else:
lines.append(line)
fname = infhandle.name
infhandle.close()
fhandle = open(fname,'w')
fhandle.write( ''.join(lines) )
fhandle.close()
infhandle = open(fname,'r')
return infhandle
#
# find line # with expression
#
def find_line_num_with_expression(infhandle,exp):
rexp = re.compile(exp)
infhandle.seek(0)
iline = 0
for line in infhandle.readlines():
if rexp.search(line):
return iline
iline = iline + 1
return None
#
# does line # contain expression
#
def does_line_num_contain_expression(infhandle,lnum,exp):
rexp = re.compile(exp)
infhandle.seek(0)
iline = 0
for line in infhandle.readlines():
if iline == lnum and rexp.search(line):
return True
elif iline == lnum:
return False
iline = iline + 1
return False
#
# insert macro...
#
def insert_macro_and_save_file(fhandle,mname,mline):
fhandle.seek(0)
fname = fhandle.name
flines = fhandle.readlines()
fhandle.close()
fmacro = write_macro(mname)
flines.insert(mline,fmacro)
fhandle = open(fname,'w')
fhandle.write(''.join(flines))
fhandle.close()
fhandle = open(fname,'r')
return fhandle
#
# Clean up
#
def clean(file_name):
infhandle = open(file_name,'r')
# grab the macro name
macro_short_string = is_using_macro(infhandle)
if not macro_short_string:
print "Unable to find a macro to clean up in file ",file_name
return False
# get rid of all the macro calls
infhandle.seek(0)
ereg = macro_short_string + "[^\s]+;$"
infhandle = remove_lines_with_expression(infhandle,ereg)
# now get rid of the macro definition itself
ereg = macro_short_string + "[\(a-z]+"
macro_def_line = find_line_num_with_expression(infhandle,ereg)
macro_end_line = does_line_num_contain_expression(infhandle,macro_def_line+1,"__FILE__,__LINE__")
if macro_end_line:
# we've found our macro def... get rid of two lines
infhandle.seek(0)
flines = infhandle.readlines()
flines.remove(flines[macro_def_line])
flines.remove(flines[macro_def_line])
infhandle.close()
infhandle = open(file_name,'w')
infhandle.write(''.join(flines))
infhandle.close()
else:
print "Unable to find the macro definition for ",file_name
return False
return True
#
# Main function
#
def main(file_name):
# open the file
infhandle = open( file_name, 'r' )
# determine a candidate for the macro definition
name = find_macro_candidate(infhandle)
if name == None:
print "Couldn't find a macro-name candidate for file ",file_name
return False
# find a good insertion point for the DEBUG macro
last_line_of_header = find_header(infhandle)
# insert the macro to enable logging...
infhandle = insert_macro_and_save_file(infhandle,name,last_line_of_header)
# now comes the more interesting parts
# ideally we allow certain functions to be instrumented
# etc, but let's leave that for another day
# so instead we insert our debug function into the first
# in a function definition.
infhandle.seek(0)
in_c_func = False
insertion_pts = []
line_index = 0
for line in infhandle:
# iterate through each line in our file and determine if we're at
# the start of a C function
ltple = find_c_function_declaration(line)
if ltple == None:
decl = 'none'
else:
decl = ltple[2]
if decl == 'definition':
# we've found a C/C++ style definition
# if the last (non-white space) character is { then
# we're in business to add our macro call,
in_c_func = True
if in_c_func and (is_trailing_char_bracket(line) or is_leading_char_bracket(line)):
in_c_func = False
# record the insertion point
insertion_pts.append(line_index+1)
# increment the line index
line_index = line_index + 1
#
# rewind the file... read it as an array and insert the macro at each insertion point
#
infhandle.seek(0)
filelines = infhandle.readlines()
macrostr = write_macro_call(name)
ii = 0
for index in insertion_pts:
filelines.insert(index + ii,macrostr)
# increment the offset
ii = ii + 1
infhandle.close()
infhandle = open(file_name,'w')
infhandle.write( ''.join(filelines) )
infhandle.close()
return True
#
# use the script as a program
#
if __name__ == "__main__":
nargs = len(sys.argv)
if nargs < 2 or nargs > 3:
print_usage()
sys.exit(1)
try:
optlist, args = getopt.getopt(sys.argv[1:],'x',['clean','list'])
except getopt.GetoptError, err:
print str(err)
print_usage()
filename = args[0]
opts = None
if nargs == 3:
opts = args[1]
if opts == '--clean':
clean(filename)
elif opts == '--list':
print "This isn't built just yet..."
else:
main(filename)