1#!/usr/bin/env python 2 3import sys 4 5header_fname = sys.argv[1] 6output_fname = sys.argv[2] 7 8w = open(output_fname, 'w') 9 10w.write(''' 11const char * 12wdns_rcode_to_str(uint16_t rcode) 13{ 14 switch (rcode) { 15''') 16 17for line in open(header_fname): 18 if 'WDNS_R_' in line: 19 wdns_type = line.split()[1] 20 rcode = wdns_type.replace('WDNS_R_','',1).replace('_', '-') 21 w.write('\tcase %s: return ("%s");\n' % (wdns_type, rcode)) 22 23w.write(''' } 24 25 return (NULL); 26} 27''') 28 29w.write(''' 30static struct u16str { 31 uint16_t u16; 32 const char *str; 33} rcodes[] = { 34''') 35 36rcodes = [] 37for line in open(header_fname): 38 if 'WDNS_R_' in line: 39 wdns_type = line.split()[1] 40 rcode = wdns_type.replace('WDNS_R_','',1).replace('_', '-') 41 rcodes.append((wdns_type, rcode)) 42rcodes.sort() 43for wdns_type, rcode in rcodes: 44 w.write('\t{ %s, "%s" },\n' % (wdns_type, rcode)) 45 46w.write('''}; 47 48#define num_rcodes (sizeof(rcodes) / sizeof(struct u16str)) 49 50static int 51cmp_u16str(const void *a, const void *b) { 52 struct u16str *u1 = (struct u16str *) a; 53 struct u16str *u2 = (struct u16str *) b; 54 return (strcasecmp(u1->str, u2->str)); 55} 56 57wdns_res 58wdns_str_to_rcode(const char *str, uint16_t *out) { 59 struct u16str key, *res; 60 key.str = str; 61 res = bsearch(&key, rcodes, num_rcodes, sizeof(struct u16str), cmp_u16str); 62 if (res != NULL) { 63 *out = res->u16; 64 return (wdns_res_success); 65 } 66 return (wdns_res_failure); 67} 68''') 69 70w.close() 71