1996-09-05 12:22:16 -03:00
|
|
|
"""Parse sys/errno.h and Errors.h and create Estr resource"""
|
|
|
|
|
|
|
|
import regex
|
|
|
|
import macfs
|
1995-02-05 13:01:45 -04:00
|
|
|
import string
|
1996-09-05 12:22:16 -03:00
|
|
|
import Res
|
|
|
|
import os
|
1995-02-05 13:01:45 -04:00
|
|
|
|
|
|
|
READ = 1
|
|
|
|
WRITE = 2
|
|
|
|
smAllScripts = -3
|
|
|
|
|
1996-09-05 12:22:16 -03:00
|
|
|
ERRNO_PROG="#define[ \t]+" \
|
|
|
|
"\([A-Z0-9a-z_]+\)" \
|
|
|
|
"[ \t]+" \
|
|
|
|
"\([0-9]+\)" \
|
|
|
|
"[ \t]*/\*[ \t]*" \
|
|
|
|
"\(.*\)" \
|
|
|
|
"[ \t]*\*/"
|
|
|
|
|
|
|
|
ERRORS_PROG="[ \t]*" \
|
|
|
|
"\([A-Z0-9a-z_]+\)" \
|
|
|
|
"[ \t]*=[ \t]*" \
|
|
|
|
"\([-0-9]+\)" \
|
|
|
|
"[, \t]*/\*[ \t]*" \
|
|
|
|
"\(.*\)" \
|
|
|
|
"[ \t]*\*/"
|
|
|
|
|
1995-02-05 13:01:45 -04:00
|
|
|
def Pstring(str):
|
|
|
|
if len(str) > 255:
|
|
|
|
raise ValueError, 'String too large'
|
|
|
|
return chr(len(str))+str
|
|
|
|
|
|
|
|
def writeestr(dst, edict):
|
|
|
|
"""Create Estr resource file given a dictionary of errors."""
|
|
|
|
|
1996-09-05 12:22:16 -03:00
|
|
|
os.unlink(dst.as_pathname())
|
|
|
|
Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
|
|
|
|
output = Res.FSpOpenResFile(dst, WRITE)
|
|
|
|
Res.UseResFile(output)
|
1995-02-05 13:01:45 -04:00
|
|
|
for num in edict.keys():
|
1996-09-05 12:22:16 -03:00
|
|
|
res = Res.Resource(Pstring(edict[num][0]))
|
1995-02-05 13:01:45 -04:00
|
|
|
res.AddResource('Estr', num, '')
|
|
|
|
res.WriteResource()
|
1996-09-05 12:22:16 -03:00
|
|
|
Res.CloseResFile(output)
|
|
|
|
|
|
|
|
def writepython(fp, dict):
|
|
|
|
k = dict.keys()
|
|
|
|
k.sort()
|
|
|
|
for i in k:
|
|
|
|
fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
|
|
|
|
|
|
|
|
|
|
|
|
def parse_errno_h(fp, dict):
|
|
|
|
errno_prog = regex.compile(ERRNO_PROG)
|
|
|
|
for line in fp.readlines():
|
|
|
|
if errno_prog.match(line) > 0:
|
|
|
|
number = string.atoi(errno_prog.group(2))
|
|
|
|
name = errno_prog.group(1)
|
|
|
|
desc = string.strip(errno_prog.group(3))
|
1995-02-05 13:01:45 -04:00
|
|
|
|
1996-09-05 12:22:16 -03:00
|
|
|
if not dict.has_key(number):
|
|
|
|
dict[number] = desc, name
|
|
|
|
else:
|
|
|
|
print 'DUPLICATE', number
|
|
|
|
print '\t', dict[number]
|
|
|
|
print '\t', (desc, name)
|
|
|
|
|
|
|
|
def parse_errors_h(fp, dict):
|
|
|
|
errno_prog = regex.compile(ERRORS_PROG)
|
|
|
|
for line in fp.readlines():
|
|
|
|
if errno_prog.match(line) > 0:
|
|
|
|
number = string.atoi(errno_prog.group(2))
|
|
|
|
name = errno_prog.group(1)
|
|
|
|
desc = string.strip(errno_prog.group(3))
|
|
|
|
if number > 0: continue
|
|
|
|
|
|
|
|
if not dict.has_key(number):
|
|
|
|
dict[number] = desc, name
|
|
|
|
else:
|
|
|
|
print 'DUPLICATE', number
|
|
|
|
print '\t', dict[number]
|
|
|
|
print '\t', (desc, name)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
dict = {}
|
|
|
|
fss, ok = macfs.PromptGetFile("Where is errno.h?")
|
|
|
|
if not ok: return
|
|
|
|
fp = open(fss.as_pathname())
|
|
|
|
parse_errno_h(fp, dict)
|
|
|
|
fp.close()
|
|
|
|
|
1997-05-15 08:18:13 -03:00
|
|
|
fss, ok = macfs.PromptGetFile("Select 2nd errno.h or cancel")
|
|
|
|
if not ok: return
|
|
|
|
fp = open(fss.as_pathname())
|
|
|
|
parse_errno_h(fp, dict)
|
|
|
|
fp.close()
|
|
|
|
|
1996-09-05 12:22:16 -03:00
|
|
|
fss, ok = macfs.PromptGetFile("Where is Errors.h?")
|
|
|
|
if not ok: return
|
|
|
|
fp = open(fss.as_pathname())
|
|
|
|
parse_errors_h(fp, dict)
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
if not dict:
|
|
|
|
return
|
|
|
|
|
|
|
|
fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
|
|
|
|
if ok:
|
|
|
|
writeestr(fss, dict)
|
|
|
|
|
|
|
|
fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
|
|
|
|
if ok:
|
|
|
|
fp = open(fss.as_pathname(), "w")
|
|
|
|
writepython(fp, dict)
|
|
|
|
fp.close()
|
|
|
|
fss.SetCreatorType('Pyth', 'TEXT')
|
|
|
|
|
|
|
|
fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
|
|
|
|
if ok:
|
|
|
|
fp = open(fss.as_pathname(), "w")
|
|
|
|
|
|
|
|
k = dict.keys()
|
|
|
|
k.sort()
|
|
|
|
for i in k:
|
|
|
|
fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
|
|
|
|
fp.close()
|
|
|
|
|
1995-02-05 13:01:45 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
1996-09-05 12:22:16 -03:00
|
|
|
main()
|
|
|
|
|