DEADSOFTWARE

old changes removed
[bbcp.git] / BlackBox / _OpenBSD_ / Lin / Mod / gen-Libc / dumpdefs.py
1 #! /usr/bin/env python2.7
2 #
3 # A. V. Shiryaev, 2012.09
4 # simple *.h '#define' macro translator
6 import sys, re
8 INDENT = "\t"
10 _p1 = re.compile('^#define\s+([^\s]+)\s+([A-Za-z0-9]+)\s*$')
11 _p2 = re.compile('^#define\s+([^\s]+)\s+([^\s]+)\s+\/\*([^\*]+)\*\/\s*$')
13 def mkset (x):
14 if x >= 0:
15 s = []
16 ss = 0
17 i = 0
18 while x != 0:
19 if x % 2 != 0:
20 if ss == 0:
21 si = i
22 sn = 1
23 ss = 1
24 elif i == si + sn:
25 sn = sn + 1
26 else:
27 if sn == 1:
28 s.append(str(si))
29 elif sn == 2:
30 s.append("%s,%s" % (si, si+1))
31 else:
32 s.append("%s..%s" % (si, si + sn - 1))
33 si = i
34 sn = 1
35 i = i + 1
36 x = x / 2
37 if ss == 1:
38 if sn == 1:
39 s.append(str(si))
40 elif sn == 2:
41 s.append("%s,%s" % (si, si+1))
42 else:
43 s.append("%s..%s" % (si, si + sn - 1))
44 return '{' + ','.join(s) + '}'
45 else:
46 return None
48 def fixVal (x, tip):
49 if x.startswith('(') and x.endswith(')') and (x.count('(') == 1) and (x.count(')') == 1):
50 x = x[1:-1]
52 if x.startswith('0x'): # fix HEX
53 if tip == 'i':
54 if x[2] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
55 return x[2:].upper() + "H"
56 else:
57 return "0" + x[2:].upper() + "H"
58 elif tip == 's':
59 return mkset(int(x, 16))
60 elif tip == 'c':
61 if x[2] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
62 return x[2:].upper() + "X"
63 else:
64 return "0" + x[2:].upper() + "X"
65 else:
66 assert False
67 elif x[0] == '0': # fix OCT
68 if tip == 'i':
69 return str(int(x, 8))
70 elif tip == 's':
71 return mkset(int(x, 8))
72 elif tip == 'c':
73 return fixVal("0x%x" % (int(x, 8),), tip)
74 else:
75 assert False
76 else:
77 if tip == 'i':
78 return x
79 else:
80 try:
81 y = int(x)
82 except:
83 return x
84 else:
85 if tip == 's':
86 return mkset(y)
87 elif tip == 'c':
88 return fixVal("0x%x" % (int(y),), tip)
89 else:
90 assert False
92 def p (nIndents, sym, val, comment, val1):
93 r = [ "%s%s* = %s;" % (INDENT*nIndents, sym, val) ]
94 if (val1 != None) and (val != val1):
95 r.append( "(* %s *)" % (val1,) )
96 if comment:
97 r.append( "(* %s *)" % (comment,) )
98 print ' '.join(r)
100 def isBits (s):
101 return (len(s) > 1) and s.startswith('0') # HEX or OCT
103 def main ():
104 if len(sys.argv) == 5:
105 nIndents = int(sys.argv[1])
106 if sys.argv[2] == '*':
107 level = None
108 else:
109 level = int(sys.argv[2])
110 tip = sys.argv[3]
111 assert tip in ('i', 's', 'c')
113 fh = open(sys.argv[4], 'r')
114 st = 0
115 for line in fh.readlines():
116 if line.startswith("#if"):
117 # print line,
118 st = st + 1
119 elif line.startswith("#endif"):
120 # print line,
121 st = st - 1
122 elif (level == None) or (st == level):
123 r = _p2.match(line)
124 if r:
125 sym = r.group(1)
126 val = fixVal(r.group(2), tip)
127 if isBits(r.group(2)):
128 val1 = fixVal(r.group(2), 's')
129 else:
130 val1 = None
131 comment = r.group(3).strip()
132 p(nIndents, sym, val, comment, val1)
133 else:
134 r = _p1.match(line)
135 if r:
136 sym = r.group(1)
137 val = fixVal(r.group(2), tip)
138 if isBits(r.group(2)):
139 val1 = fixVal(r.group(2), 's')
140 else:
141 val1 = None
142 comment = None
143 p(nIndents, sym, val, comment, val1)
144 else:
145 print "usage: %s nIndents level type file.h" % (sys.argv[0],)
146 print " level:"
147 print " number: selected level"
148 print " *: all levels"
149 print " type:"
150 print " i: integer"
151 print " s: set"
152 print " c: shortchar"
154 if __name__ == '__main__':
155 main()