blob: 3ed3b34f6df89ac9d6f848effe5870d9c75812b2 [file] [log] [blame]
Jack Jansenac4d8691996-09-05 15:22:16 +00001"""Parse sys/errno.h and Errors.h and create Estr resource"""
2
Jack Jansenbe614ee2001-01-02 22:02:02 +00003import re
Jack Jansenac4d8691996-09-05 15:22:16 +00004import 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]+" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000014 "([A-Z0-9a-z_]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000015 "[ \t]+" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000016 "([0-9]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000017 "[ \t]*/\*[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000018 "(.*)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000019 "[ \t]*\*/"
20
21ERRORS_PROG="[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000022 "([A-Z0-9a-z_]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000023 "[ \t]*=[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000024 "([-0-9]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000025 "[, \t]*/\*[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000026 "(.*)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000027 "[ \t]*\*/"
Jack Jansen955a2f51998-04-21 15:27:45 +000028
29ERRORS_PROG_2="[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000030 "([A-Z0-9a-z_]+)" \
Jack Jansen955a2f51998-04-21 15:27:45 +000031 "[ \t]*=[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000032 "([-0-9]+)" \
Jack Jansen955a2f51998-04-21 15:27:45 +000033 "[, \t]*"
Jack Jansenac4d8691996-09-05 15:22:16 +000034
Guido van Rossum3c3eda21995-02-05 17:01:45 +000035def Pstring(str):
36 if len(str) > 255:
37 raise ValueError, 'String too large'
38 return chr(len(str))+str
39
40def writeestr(dst, edict):
41 """Create Estr resource file given a dictionary of errors."""
42
Jack Jansenac4d8691996-09-05 15:22:16 +000043 os.unlink(dst.as_pathname())
44 Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
45 output = Res.FSpOpenResFile(dst, WRITE)
46 Res.UseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000047 for num in edict.keys():
Jack Jansenac4d8691996-09-05 15:22:16 +000048 res = Res.Resource(Pstring(edict[num][0]))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000049 res.AddResource('Estr', num, '')
50 res.WriteResource()
Jack Jansenac4d8691996-09-05 15:22:16 +000051 Res.CloseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000052
Jack Jansenac4d8691996-09-05 15:22:16 +000053def writepython(fp, dict):
54 k = dict.keys()
55 k.sort()
56 for i in k:
57 fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
58
59
60def parse_errno_h(fp, dict):
Jack Jansenbe614ee2001-01-02 22:02:02 +000061 errno_prog = re.compile(ERRNO_PROG)
Jack Jansenac4d8691996-09-05 15:22:16 +000062 for line in fp.readlines():
Jack Jansenbe614ee2001-01-02 22:02:02 +000063 m = errno_prog.match(line)
64 if m:
65 number = string.atoi(m.group(2))
66 name = m.group(1)
67 desc = string.strip(m.group(3))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000068
Jack Jansenac4d8691996-09-05 15:22:16 +000069 if not dict.has_key(number):
70 dict[number] = desc, name
71 else:
72 print 'DUPLICATE', number
73 print '\t', dict[number]
74 print '\t', (desc, name)
75
76def parse_errors_h(fp, dict):
Jack Jansenbe614ee2001-01-02 22:02:02 +000077 errno_prog = re.compile(ERRORS_PROG)
78 errno_prog_2 = re.compile(ERRORS_PROG_2)
Jack Jansenac4d8691996-09-05 15:22:16 +000079 for line in fp.readlines():
Jack Jansen955a2f51998-04-21 15:27:45 +000080 match = 0
Jack Jansenbe614ee2001-01-02 22:02:02 +000081 m = errno_prog.match(line)
82 m2 = errno_prog_2.match(line)
83 if m:
84 number = string.atoi(m.group(2))
85 name = m.group(1)
86 desc = string.strip(m.group(3))
Jack Jansen955a2f51998-04-21 15:27:45 +000087 match=1
Jack Jansenbe614ee2001-01-02 22:02:02 +000088 elif m2:
89 number = string.atoi(m2.group(2))
90 name = m2.group(1)
Jack Jansen955a2f51998-04-21 15:27:45 +000091 desc = name
92 match=1
93 if match:
Jack Jansenac4d8691996-09-05 15:22:16 +000094 if number > 0: continue
95
96 if not dict.has_key(number):
97 dict[number] = desc, name
98 else:
99 print 'DUPLICATE', number
100 print '\t', dict[number]
101 print '\t', (desc, name)
Jack Jansen955a2f51998-04-21 15:27:45 +0000102 if len(desc) > len(dict[number][0]):
103 print 'Pick second one'
104 dict[number] = desc, name
Jack Jansenac4d8691996-09-05 15:22:16 +0000105
106def main():
107 dict = {}
Jack Jansenc158bb22001-01-01 22:57:59 +0000108 fss, ok = macfs.PromptGetFile("Where is GUSI sys/errno.h?")
Jack Jansenac4d8691996-09-05 15:22:16 +0000109 if not ok: return
110 fp = open(fss.as_pathname())
111 parse_errno_h(fp, dict)
112 fp.close()
113
Jack Jansenc158bb22001-01-01 22:57:59 +0000114 fss, ok = macfs.PromptGetFile("Select 2nd errno.h (MSL) or cancel")
Jack Jansenb83b46d1997-05-15 11:18:13 +0000115 if not ok: return
116 fp = open(fss.as_pathname())
117 parse_errno_h(fp, dict)
118 fp.close()
119
Jack Jansenac4d8691996-09-05 15:22:16 +0000120 fss, ok = macfs.PromptGetFile("Where is Errors.h?")
121 if not ok: return
122 fp = open(fss.as_pathname())
123 parse_errors_h(fp, dict)
124 fp.close()
125
126 if not dict:
127 return
128
129 fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
130 if ok:
131 writeestr(fss, dict)
132
133 fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
134 if ok:
135 fp = open(fss.as_pathname(), "w")
136 writepython(fp, dict)
137 fp.close()
138 fss.SetCreatorType('Pyth', 'TEXT')
139
140 fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
141 if ok:
142 fp = open(fss.as_pathname(), "w")
143
144 k = dict.keys()
145 k.sort()
146 for i in k:
147 fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
148 fp.close()
149
Guido van Rossum3c3eda21995-02-05 17:01:45 +0000150
151if __name__ == '__main__':
Jack Jansenac4d8691996-09-05 15:22:16 +0000152 main()
153