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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
"""\
Tools for scanning header files in search of function prototypes.
Often, the function prototypes in header files contain enough information
to automatically generate (or reverse-engineer) interface specifications
from them. The conventions used are very vendor specific, but once you've
figured out what they are they are often a great help, and it sure beats
manually entering the interface specifications. (These are needed to generate
the glue used to access the functions from Python.)
In order to make this class useful, almost every component can be overridden.
The defaults are (currently) tuned to scanning Apple Macintosh header files,
although most Mac specific details are contained in header-specific subclasses.
"""
import regex
import regsub
import string
import sys
import os
import fnmatch
from types import *
try:
import MacOS
except ImportError:
MacOS = None
# Default preferences
CREATOR = 'KAHL' # My favorite text editor on the Mac
INCLUDEDIR = "D:Development:THINK C:Mac #includes:Apple #includes:"
Error = "scantools.Error"
class Scanner:
def __init__(self, input = None, output = None, defsoutput = None):
self.initsilent()
self.initblacklists()
self.initrepairinstructions()
self.initpaths()
self.initfiles()
self.initpatterns()
self.compilepatterns()
self.initosspecifics()
if output:
self.setoutput(output, defsoutput)
if input:
self.setinput(input)
def initsilent(self):
self.silent = 0
def error(self, format, *args):
if self.silent >= 0:
print format%args
def report(self, format, *args):
if not self.silent:
print format%args
def initblacklists(self):
self.blacklistnames = self.makeblacklistnames()
self.blacklisttypes = ["unknown", "-"] + self.makeblacklisttypes()
def makeblacklistnames(self):
return []
def makeblacklisttypes(self):
return []
def initrepairinstructions(self):
self.repairinstructions = self.makerepairinstructions()
def makerepairinstructions(self):
"""Parse the repair file into repair instructions.
The file format is simple:
1) use \ to split a long logical line in multiple physical lines
2) everything after the first # on a line is ignored (as comment)
3) empty lines are ignored
4) remaining lines must have exactly 3 colon-separated fields:
functionpattern : argumentspattern : argumentsreplacement
5) all patterns use shell style pattern matching
6) an empty functionpattern means the same as *
7) the other two fields are each comma-separated lists of triples
8) a triple is a space-separated list of 1-3 words
9) a triple with less than 3 words is padded at the end with "*" words
10) when used as a pattern, a triple matches the type, name, and mode
of an argument, respectively
11) when used as a replacement, the words of a triple specify
replacements for the corresponding words of the argument,
with "*" as a word by itself meaning leave the original word
(no other uses of "*" is allowed)
12) the replacement need not have the same number of triples
as the pattern
"""
f = self.openrepairfile()
if not f: return []
print "Reading repair file", `f.name`, "..."
list = []
lineno = 0
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
startlineno = lineno
while line[-2:] == '\\\n':
line = line[:-2] + ' ' + f.readline()
lineno = lineno + 1
i = string.find(line, '#')
if i >= 0: line = line[:i]
words = map(string.strip, string.splitfields(line, ':'))
if words == ['']: continue
if len(words) <> 3:
print "Line", startlineno,
print ": bad line (not 3 colon-separated fields)"
print `line`
continue
[fpat, pat, rep] = words
if not fpat: fpat = "*"
if not pat:
print "Line", startlineno,
print "Empty pattern"
print `line`
continue
patparts = map(string.strip, string.splitfields(pat, ','))
repparts = map(string.strip, string.splitfields(rep, ','))
patterns = []
for p in patparts:
if not p:
print "Line", startlineno,
print "Empty pattern part"
print `line`
continue
pattern = string.split(p)
if len(pattern) > 3:
print "Line", startlineno,
print "Pattern part has > 3 words"
print `line`
pattern = pattern[:3]
else:
while len(pattern) < 3:
pattern.append("*")
patterns.append(pattern)
replacements = []
for p in repparts:
if not p:
print "Line", startlineno,
print "Empty replacement part"
print `line`
continue
replacement = string.split(p)
if len(replacement) > 3:
print "Line", startlineno,
print "Pattern part has > 3 words"
print `line`
replacement = replacement[:3]
else:
while len(replacement) < 3:
replacement.append("*")
replacements.append(replacement)
list.append((fpat, patterns, replacements))
return list
def openrepairfile(self, filename = "REPAIR"):
try:
return open(filename, "r")
except IOError, msg:
print `filename`, ":", msg
print "Cannot open repair file -- assume no repair needed"
return None
def initfiles(self):
self.specmine = 0
self.defsmine = 0
self.scanmine = 0
self.specfile = sys.stdout
self.defsfile = None
self.scanfile = sys.stdin
self.lineno = 0
self.line = ""
def initpaths(self):
self.includepath = [':', INCLUDEDIR]
def initpatterns(self):
self.head_pat = "^pascal[ \t]+" # XXX Mac specific!
self.tail_pat = "[;={}]"
self.type_pat = "pascal[ \t\n]+\(<type>[a-zA-Z0-9_]+\)[ \t\n]+"
self.name_pat = "\(<name>[a-zA-Z0-9_]+\)[ \t\n]*"
self.args_pat = "(\(<args>\([^(;=)]+\|([^(;=)]*)\)*\))"
self.whole_pat = self.type_pat + self.name_pat + self.args_pat
self.sym_pat = "^[ \t]*\(<name>[a-zA-Z0-9_]+\)[ \t]*=" + \
"[ \t]*\(<defn>[-0-9'\"][^\t\n,;}]*\),?"
self.asplit_pat = "^\(<type>.*[^a-zA-Z0-9_]\)\(<name>[a-zA-Z0-9_]+\)$"
def compilepatterns(self):
for name in dir(self):
if name[-4:] == "_pat":
pat = getattr(self, name)
prog = regex.symcomp(pat)
setattr(self, name[:-4], prog)
def initosspecifics(self):
if MacOS:
self.filetype = 'TEXT'
self.filecreator = CREATOR
else:
self.filetype = self.filecreator = None
def setfiletype(self, filename):
if MacOS and (self.filecreator or self.filetype):
creator, type = MacOS.GetCreatorAndType(filename)
if self.filecreator: creator = self.filecreator
if self.filetype: type = self.filetype
MacOS.SetCreatorAndType(filename, creator, type)
def close(self):
self.closefiles()
def closefiles(self):
self.closespec()
self.closedefs()
self.closescan()
def closespec(self):
tmp = self.specmine and self.specfile
self.specfile = None
if tmp: tmp.close()
def closedefs(self):
tmp = self.defsmine and self.defsfile
self.defsfile = None
if tmp: tmp.close()
def closescan(self):
tmp = self.scanmine and self.scanfile
self.scanfile = None
if tmp: tmp.close()
def setoutput(self, spec, defs = None):
self.closespec()
self.closedefs()
if spec:
if type(spec) == StringType:
file = self.openoutput(spec)
mine = 1
else:
file = spec
mine = 0
self.specfile = file
self.specmine = mine
if defs:
if type(defs) == StringType:
file = self.openoutput(defs)
mine = 1
else:
file = defs
mine = 0
self.defsfile = file
self.defsmine = mine
def openoutput(self, filename):
file = open(filename, 'w')
self.setfiletype(filename)
return file
def setinput(self, scan = sys.stdin):
self.closescan()
if scan:
if type(scan) == StringType:
file = self.openinput(scan)
mine = 1
else:
file = scan
mine = 0
self.scanfile = file
self.scanmine = mine
self.lineno = 0
def openinput(self, filename):
if not os.path.isabs(filename):
for dir in self.includepath:
fullname = os.path.join(dir, filename)
#self.report("trying full name %s", `fullname`)
try:
return open(fullname, 'r')
except IOError:
pass
# If not on the path, or absolute, try default open()
return open(filename, 'r')
def getline(self):
if not self.scanfile:
raise Error, "input file not set"
self.line = self.scanfile.readline()
if not self.line:
raise EOFError
self.lineno = self.lineno + 1
return self.line
def scan(self):
if not self.scanfile:
self.error("No input file has been specified")
return
inputname = self.scanfile.name
self.report("scanfile = %s", `inputname`)
if not self.specfile:
self.report("(No interface specifications will be written)")
else:
self.report("specfile = %s", `self.specfile.name`)
self.specfile.write("# Generated from %s\n\n" % `inputname`)
if not self.defsfile:
self.report("(No symbol definitions will be written)")
else:
self.report("defsfile = %s", `self.defsfile.name`)
self.defsfile.write("# Generated from %s\n\n" % `inputname`)
self.alreadydone = []
try:
while 1:
try: line = self.getline()
except EOFError: break
if self.defsfile and self.sym.match(line) >= 0:
self.dosymdef()
continue
if self.head.match(line) >= 0:
self.dofuncspec()
continue
except EOFError:
self.error("Uncaught EOF error")
def dosymdef(self):
name, defn = self.sym.group('name', 'defn')
self.defsfile.write("%s = %s\n" % (name, defn))
def dofuncspec(self):
raw = self.line
while self.tail.search(raw) < 0:
line = self.getline()
raw = raw + line
self.processrawspec(raw)
def processrawspec(self, raw):
if self.whole.search(raw) < 0:
self.report("Bad raw spec: %s", `raw`)
return
type, name, args = self.whole.group('type', 'name', 'args')
if name in self.alreadydone:
self.report("Name has already been defined: %s", `name`)
return
self.report("==> %s %s <==", type, name)
if self.blacklisted(type, name):
self.error("*** %s %s blacklisted", type, name)
return
arglist = self.extractarglist(args)
arglist = self.repairarglist(name, arglist)
if self.unmanageable(type, name, arglist):
##for arg in arglist:
## self.report(" %s", `arg`)
self.error("*** %s %s unmanageable", type, name)
return
self.alreadydone.append(name)
self.generate(type, name, arglist)
def extractarglist(self, args):
args = string.strip(args)
if not args or args == "void":
return []
parts = map(string.strip, string.splitfields(args, ","))
arglist = []
for part in parts:
arg = self.extractarg(part)
arglist.append(arg)
return arglist
def extractarg(self, part):
mode = "InMode"
if self.asplit.match(part) < 0:
self.error("Indecipherable argument: %s", `part`)
return ("unknown", part, mode)
type, name = self.asplit.group('type', 'name')
type = regsub.gsub("\*", " ptr ", type)
type = string.strip(type)
type = regsub.gsub("[ \t]+", "_", type)
return self.modifyarg(type, name, mode)
def modifyarg(self, type, name, mode):
if type[:6] == "const_":
type = type[6:]
elif type[-4:] == "_ptr":
type = type[:-4]
mode = "OutMode"
if type[-4:] == "_far":
type = type[:-4]
return type, name, mode
def repairarglist(self, functionname, arglist):
arglist = arglist[:]
i = 0
while i < len(arglist):
for item in self.repairinstructions:
if len(item) == 2:
pattern, replacement = item
functionpat = "*"
else:
functionpat, pattern, replacement = item
if not fnmatch.fnmatchcase(functionname, functionpat):
continue
n = len(pattern)
if i+n > len(arglist): continue
current = arglist[i:i+n]
for j in range(n):
if not self.matcharg(pattern[j], current[j]):
break
else: # All items of the pattern match
new = self.substituteargs(
pattern, replacement, current)
if new is not None:
arglist[i:i+n] = new
i = i+len(new) # No recursive substitutions
break
else: # No patterns match
i = i+1
return arglist
def matcharg(self, patarg, arg):
return len(filter(None, map(fnmatch.fnmatchcase, arg, patarg))) == 3
def substituteargs(self, pattern, replacement, old):
new = []
for k in range(len(replacement)):
item = replacement[k]
newitem = [item[0], item[1], item[2]]
for i in range(3):
if item[i] == '*':
newitem[i] = old[k][i]
elif item[i][:1] == '$':
index = string.atoi(item[i][1:]) - 1
newitem[i] = old[index][i]
new.append(tuple(newitem))
##self.report("old: %s", `old`)
##self.report("new: %s", `new`)
return new
def generate(self, type, name, arglist):
classname, listname = self.destination(type, name, arglist)
if not self.specfile: return
self.specfile.write("f = %s(%s, %s,\n" % (classname, type, `name`))
for atype, aname, amode in arglist:
self.specfile.write(" (%s, %s, %s),\n" %
(atype, `aname`, amode))
self.specfile.write(")\n")
self.specfile.write("%s.append(f)\n\n" % listname)
def destination(self, type, name, arglist):
return "FunctionGenerator", "functions"
def blacklisted(self, type, name):
if type in self.blacklisttypes:
##self.report("return type %s is blacklisted", type)
return 1
if name in self.blacklistnames:
##self.report("function name %s is blacklisted", name)
return 1
return 0
def unmanageable(self, type, name, arglist):
for atype, aname, amode in arglist:
if atype in self.blacklisttypes:
self.report("argument type %s is blacklisted", atype)
return 1
return 0
def test():
input = "D:Development:THINK C:Mac #includes:Apple #includes:AppleEvents.h"
output = "@aespecs.py"
defsoutput = "@aedefs.py"
s = Scanner(input, output, defsoutput)
s.scan()
if __name__ == '__main__':
test()
|