from BeautifulSoup import BeautifulSoup tables = dict( tab_single='single.html', ) types = dict( VD = 'DREX_' ) imm = ( 'A', 'I', 'J', 'O', ) for name in imm: types[name] = 'IMM_' modrm = ( 'C', 'D', 'E', 'F', 'G', 'M', 'P', 'PR', 'N', 'Q', 'R', 'S', 'T', 'V', 'VR', 'U', 'W', 'X', 'Y' ) for name in modrm: types[name] = 'MODRM_' sizes = dict( b = 'BYTE', w = 'WORD', d = 'DWORD', q = 'QWORD', o = 'WORD', v = 'VAR', z = 'VAR', a = 'VAR', p = 'POINTER', s = 'SIXBYTE', t = 'TENBYTE', ) def parse(val): flags = [] for sub in val.split(','): sub = sub.strip() if not sub or len(sub) < 2: continue sub = sub[:-1], sub[-1] if len(sub) != 2 or sub[1].lower() != sub[1] or sub[0].upper() != sub[0]: continue if sub[0][0] == '(' or sub[1] == ':': continue if sub[1] in '0123456789': continue type = types[sub[0]] size = sizes[sub[1]] if not isinstance(type, list) and not isinstance(type, tuple): type = [type] for subtype in type: flags.append(subtype + size) if not flags: flags = '0' return ' | '.join(flags) def screenScrape(html): ret = dict() soup = BeautifulSoup(html) for table in soup.contents: if not hasattr(table, 'name') or table.name != 'table': continue top = True lower = [] for tr in table.tbody: if not hasattr(tr, 'name') or tr.name != 'tr': continue i = 0 for td in tr: if not hasattr(td, 'name') or td.name != 'td': continue if top: for child in td.font: if not child.string or not child.string.strip(): continue try: lower.append(int(child.string[1], 16)) except: lower.append(0) else: lines = [] for child in td.font: if not child.string or not child.string.strip(): continue lines.append(child.string) if i == 0: higher = int(lines[0][0], 16) << 4 elif i > 8: continue else: key = higher | lower[i] if len(lines) < 2: continue ret[key] = ','.join(lines).replace(' ', ',') i += 1 top = False return ret for name, table in tables.items(): table = screenScrape(file(table, 'r').read()) print 'UCHAR %10s[0x100] = {' % name for i in range(0x100): if i in table: val = parse(table[i]) else: val = 0 if i == 0xFF: print '\t/* 0x%02x */ %s' % (i, val) else: print '\t/* 0x%02x */ %s,' % (i, val) print '};' print