summaryrefslogtreecommitdiffstats
path: root/Mac/scripts/mkestrres.py
blob: c19e6d0ba29c968c5fb96fe0eada46cc624e73e0 (plain)
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
"""Parse sys/errno.h and Errors.h and create Estr resource"""

import regex
import macfs
import string
import Res
import os

READ = 1
WRITE = 2
smAllScripts = -3

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]*\*/"
		   
ERRORS_PROG_2="[ \t]*" \
			"\([A-Z0-9a-z_]+\)" \
			"[ \t]*=[ \t]*" \
			"\([-0-9]+\)" \
			"[, \t]*"

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."""
	
	os.unlink(dst.as_pathname())
	Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
	output = Res.FSpOpenResFile(dst, WRITE)
	Res.UseResFile(output)
	for num in edict.keys():
		res = Res.Resource(Pstring(edict[num][0]))
		res.AddResource('Estr', num, '')
		res.WriteResource()
	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))
			
			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)
	errno_prog_2 = regex.compile(ERRORS_PROG_2)
	for line in fp.readlines():
		match = 0
		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))
			match=1
		elif errno_prog_2.match(line) > 0:
			number = string.atoi(errno_prog_2.group(2))
			name = errno_prog_2.group(1)
			desc = name
			match=1
		if match:
			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)
				if len(desc) > len(dict[number][0]):
					print 'Pick second one'
					dict[number] = desc, name
			
def main():
	dict = {}
	fss, ok = macfs.PromptGetFile("Where is GUSI sys/errno.h?")
	if not ok: return
	fp = open(fss.as_pathname())
	parse_errno_h(fp, dict)
	fp.close()
	
	fss, ok = macfs.PromptGetFile("Select 2nd errno.h (MSL) or cancel")
	if not ok: return
	fp = open(fss.as_pathname())
	parse_errno_h(fp, dict)
	fp.close()
	
	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()

	
if __name__ == '__main__':
	main()