s3e: fix build break
[calu.git] / isasty / gentex.pl
1 #!/usr/bin/perl
2
3 sub ins {
4         my $bits = $_[0];
5         my $bline;
6         #bit value line
7         my $vline;
8         my $nline;
9         #print bit value line
10         my $pvline;
11         my @bitlist;
12         my $tabular;
13
14
15         $count = 0;
16         @fields = split(/\|/, $_[3]);
17         foreach my $field (@fields) {
18                 #remove heading and trailing spaces
19                 $field =~ s/^\s+//;
20                 $field =~ s/\s+$//;
21
22                 $count += $field;
23                 #$tabular .= $field." \\\\";
24         }
25
26         if($count != $bits) {
27                 return "Num of Fields not equal to Bits!";
28         }
29         $count = 0;
30         @fieldnames = split(/\|/, $_[4]);
31
32         if(scalar(@fields) != scalar(@fieldnames)) {
33                 return "Num of Fields != Num of Names";
34         }
35
36         $pvline = 0;
37         foreach my $fieldn (@fieldnames) {
38                 #remove heading and trailing spaces
39                 $fieldn =~ s/^\s+//;
40                 $fieldn =~ s/\s+$//;
41
42                 if($fieldn =~ m/^[^\(.]*\(\s*([0-1]+)\s*\).*$/) {
43                         if(length($1) == $fields[$count]) {
44                                 push(@bitlist,$1);
45                                 $fieldn =~  s/\s?\(\s*[0-1]+\s*\)//;
46                                 $pvline = 1;
47                         }
48                         else {
49                                 $x = '';
50                                 for($i = 1; $i <= $fields[$count]; $i++) {
51                                         $x .= 'X';
52                                 }
53                         push(@bitlist,$x);
54                         }
55                 }
56                 else {
57                         $x = '';
58                         for($i = 1; $i <= $fields[$count]; $i++) {
59                                 $x .= 'X';
60                         }
61                         push(@bitlist,$x);
62                 }
63                 #$tabular .= $fieldn.": ". $bits." \\\\";
64                 $count++;
65         }
66         $bitlist = join("",@bitlist);
67
68         #$tabular .= "\\textbf{names:} @fieldnames \n";
69         #$tabular .= "\\textbf{bitlist:} $bitlist \n";
70
71         $count = $bits-1;
72
73         $tabular = "\\begin{center} \n \\resizebox{\\textwidth}{!}{\n";
74         $tabular .= "\\begin{tabular}{|l|";
75         for($i = $bits-1; $i >= 0; $i--) {
76                 #build table
77                 $tabular .= "c|";
78
79                 #build bitcount
80                 $bline .= $i;
81                 if($i != 0) {
82                         $bline .= " & ";
83                 }
84                 else {
85                         $bline .= "\\\\ \\hline \n";
86                 }
87
88                 #build bitvalue
89                 $vline .= substr($bitlist,($bits-1)-$i,1);
90                 if($i != 0) {
91                         $vline .= " & ";
92                 }
93                 else {
94                         $vline .= "\\\\ \\hline \n";
95                 }
96
97                 #build nameline
98                 #because of multicolumn we need to limit execution
99                 if($i == $count) {
100                         $nline .= "\\multicolumn{@fields[0]}{>{\\columncolor{names}}c|}{@fieldnames[0]}";
101
102                         #update count
103                         $count -= @fields[0];
104
105                         if($count <= 0) {
106                                 $nline .= "\\\\ \\hline \n";
107                         }
108                         else {
109                                 $nline .= " & ";
110                         }
111
112                         shift(@fieldnames);
113                         shift(@fields);
114                 }
115         }
116         $tabular .= "} \\hline \n";
117         $tabular .= "\\multicolumn{".($bits+1)."}{|>{\\columncolor{title}}c|}{\\textbf{".$_[1]."} (".$_[2].")} \\\\ \\hline \n";
118         $tabular .= "\\rowcolor{bit} \\cellcolor{title} &". $bline;
119         if($pvline == 1) {
120                 $tabular .= "\\rowcolor{bitval} \\cellcolor{title}Values &". $vline;
121         }
122         $tabular .= "\\cellcolor{title}Field &". $nline;
123         $tabular .= "\\end{tabular}\n}\n \\end{center}\n";
124
125
126         return $tabular;
127 }
128
129 if($#ARGV != 0) {
130         die "Usage: gentex.pl <file.ptex>";
131 }
132
133 open(DATA, "<".$ARGV[0]) || die "Couldn't open file $ARGV[0] for read!";
134
135 $output = $ARGV[0];
136
137 $output =~ s/\.[^.]*$//;
138 $output .= ".tex";
139
140 open(OUTPUT, ">".$output) || die "Couldn't open file $output for output!";
141
142 while(<DATA>)
143 {
144         $res = eval "$_";
145         print OUTPUT $res;
146 }
147
148 close(DATA);
149 close(OUTPUT);