blob: f9874f6ed90b85f0f2485384b42df9148fd8949f [file] [log] [blame]
Jack Jansenac4d8691996-09-05 15:22:16 +00001"""Parse sys/errno.h and Errors.h and create Estr resource"""
2
3import regex
4import macfs
Guido van Rossum3c3eda21995-02-05 17:01:45 +00005import string
Jack Jansenac4d8691996-09-05 15:22:16 +00006import Res
7import os
Guido van Rossum3c3eda21995-02-05 17:01:45 +00008
9READ = 1
10WRITE = 2
11smAllScripts = -3
12
Jack Jansenac4d8691996-09-05 15:22:16 +000013ERRNO_PROG="#define[ \t]+" \
14 "\([A-Z0-9a-z_]+\)" \
15 "[ \t]+" \
16 "\([0-9]+\)" \
17 "[ \t]*/\*[ \t]*" \
18 "\(.*\)" \
19 "[ \t]*\*/"
20
21ERRORS_PROG="[ \t]*" \
22 "\([A-Z0-9a-z_]+\)" \
23 "[ \t]*=[ \t]*" \
24 "\([-0-9]+\)" \
25 "[, \t]*/\*[ \t]*" \
26 "\(.*\)" \
27 "[ \t]*\*/"
28
Guido van Rossum3c3eda21995-02-05 17:01:45 +000029def Pstring(str):
30 if len(str) > 255:
31 raise ValueError, 'String too large'
32 return chr(len(str))+str
33
34def writeestr(dst, edict):
35 """Create Estr resource file given a dictionary of errors."""
36
Jack Jansenac4d8691996-09-05 15:22:16 +000037 os.unlink(dst.as_pathname())
38 Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
39 output = Res.FSpOpenResFile(dst, WRITE)
40 Res.UseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000041 for num in edict.keys():
Jack Jansenac4d8691996-09-05 15:22:16 +000042 res = Res.Resource(Pstring(edict[num][0]))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000043 res.AddResource('Estr', num, '')
44 res.WriteResource()
Jack Jansenac4d8691996-09-05 15:22:16 +000045 Res.CloseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000046
Jack Jansenac4d8691996-09-05 15:22:16 +000047def writepython(fp, dict):
48 k = dict.keys()
49 k.sort()
50 for i in k:
51 fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
52
53
54def parse_errno_h(fp, dict):
55 errno_prog = regex.compile(ERRNO_PROG)
56 for line in fp.readlines():
57 if errno_prog.match(line) > 0:
58 number = string.atoi(errno_prog.group(2))
59 name = errno_prog.group(1)
60 desc = string.strip(errno_prog.group(3))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000061
Jack Jansenac4d8691996-09-05 15:22:16 +000062 if not dict.has_key(number):
63 dict[number] = desc, name
64 else:
65 print 'DUPLICATE', number
66 print '\t', dict[number]
67 print '\t', (desc, name)
68
69def parse_errors_h(fp, dict):
70 errno_prog = regex.compile(ERRORS_PROG)
71 for line in fp.readlines():
72 if errno_prog.match(line) > 0:
73 number = string.atoi(errno_prog.group(2))
74 name = errno_prog.group(1)
75 desc = string.strip(errno_prog.group(3))
76 if number > 0: continue
77
78 if not dict.has_key(number):
79 dict[number] = desc, name
80 else:
81 print 'DUPLICATE', number
82 print '\t', dict[number]
83 print '\t', (desc, name)
84
85def main():
86 dict = {}
87 fss, ok = macfs.PromptGetFile("Where is errno.h?")
88 if not ok: return
89 fp = open(fss.as_pathname())
90 parse_errno_h(fp, dict)
91 fp.close()
92
Jack Jansenb83b46d1997-05-15 11:18:13 +000093 fss, ok = macfs.PromptGetFile("Select 2nd errno.h or cancel")
94 if not ok: return
95 fp = open(fss.as_pathname())
96 parse_errno_h(fp, dict)
97 fp.close()
98
Jack Jansenac4d8691996-09-05 15:22:16 +000099 fss, ok = macfs.PromptGetFile("Where is Errors.h?")
100 if not ok: return
101 fp = open(fss.as_pathname())
102 parse_errors_h(fp, dict)
103 fp.close()
104
105 if not dict:
106 return
107
108 fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
109 if ok:
110 writeestr(fss, dict)
111
112 fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
113 if ok:
114 fp = open(fss.as_pathname(), "w")
115 writepython(fp, dict)
116 fp.close()
117 fss.SetCreatorType('Pyth', 'TEXT')
118
119 fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
120 if ok:
121 fp = open(fss.as_pathname(), "w")
122
123 k = dict.keys()
124 k.sort()
125 for i in k:
126 fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
127 fp.close()
128
Guido van Rossum3c3eda21995-02-05 17:01:45 +0000129
130if __name__ == '__main__':
Jack Jansenac4d8691996-09-05 15:22:16 +0000131 main()
132