| 1 |
#!/usr/bin/perl |
| 2 |
# mktable - generate: |
| 3 |
# 1. struct <def-file>_type |
| 4 |
# 2. table <def-file>_table |
| 5 |
# 3. table size <def-file>_size |
| 6 |
# 3. enum-list <def-file>_enum, enumerator from given names |
| 7 |
# from one definition file |
| 8 |
# |
| 9 |
# definition file: |
| 10 |
# ^# hashes are allowed as comments in begining of line |
| 11 |
# ^# first line declares type-member pairs separated by slash |
| 12 |
# ^# typenames have to be in one part eg. "struct type" don't |
| 13 |
# ^# work, typedef these instead. |
| 14 |
# ^ type1/member1 type2/memeber2 |
| 15 |
# ^ |
| 16 |
# ^# in data lines, exist first name of row and then members |
| 17 |
# ^# of struct in order. The name may not enclose in "'s althought |
| 18 |
# ^# its type is char *. |
| 19 |
# ^ name member1 member2 |
| 20 |
# ^ name ... ... |
| 21 |
# ^ ... |
| 22 |
# |
| 23 |
|
| 24 |
if( ! $ARGV[0] ) { |
| 25 |
print "Usage: mktable definition-file\n"; |
| 26 |
exit(2); |
| 27 |
} else { |
| 28 |
$def = $ARGV[0]; |
| 29 |
} |
| 30 |
|
| 31 |
open(DEF,$def) || die("Can't open ".$def); |
| 32 |
$line = 0; |
| 33 |
$elems = 0; |
| 34 |
%table; |
| 35 |
|
| 36 |
print "/* should not be modified */\n\n"; |
| 37 |
&loop; |
| 38 |
|
| 39 |
sub loop { |
| 40 |
while(<DEF>) { |
| 41 |
chop; |
| 42 |
next if /^$/; |
| 43 |
next if /^[\t ]*#/; |
| 44 |
$line++; |
| 45 |
@tuple = split; |
| 46 |
shift @tuple if $tuple[0] eq ""; |
| 47 |
if($line == 1) { |
| 48 |
print "typedef struct {\n"; |
| 49 |
print "\tchar\t*key;\n"; |
| 50 |
foreach $field (@tuple) { |
| 51 |
# print "\"$field\"\n"; |
| 52 |
last if $field eq "empty"; |
| 53 |
($type,$mem) = split(/\//,$field); |
| 54 |
print "\t$type\t$mem;\n"; |
| 55 |
} |
| 56 |
print "} ".$def."_type;\n\n"; |
| 57 |
} else { |
| 58 |
$elems++; |
| 59 |
$key = shift(@tuple); |
| 60 |
$val = join(":",@tuple); |
| 61 |
$table{$key} = $val; |
| 62 |
} |
| 63 |
} |
| 64 |
# size |
| 65 |
print "#define\t".$def."_size\t$elems\n\n"; |
| 66 |
# table |
| 67 |
print "/* sorted */\n"; |
| 68 |
print $def."_type ".$def."_table[] = {\n"; |
| 69 |
foreach $key (sort(keys(%table))) { |
| 70 |
print "\t\"$key\",\t"; |
| 71 |
foreach $mem (split(/:/,$table{$key})) { |
| 72 |
print "$mem,\t"; |
| 73 |
} |
| 74 |
print "\n"; |
| 75 |
} |
| 76 |
print "}; /* ".$def."_table */\n\n"; |
| 77 |
# enum |
| 78 |
print "typedef enum {\n"; |
| 79 |
foreach $key (sort(keys(%table))) { |
| 80 |
print "\tinput_".$key.",\n"; |
| 81 |
} |
| 82 |
print "} "."$def"."_enum;\n\n"; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|