New build path variable
[freeradius.git] / share / format.pl
1 #!/usr/bin/env perl
2 #
3 #  Format the dictionaries according to a standard scheme.
4 #
5 #  Usage: cat dictionary | ./format.pl > new
6 #
7 #  We don't over-write the dictionaries in place, so that the process
8 #  can be double-checked by hand.
9 #
10 #  This is a bit of a hack.
11 #
12 #  FIXME: get lengths from variables, rather than hard-coding.
13 #
14 #  $Id$
15 #
16
17 $begin_vendor = 0;
18 $blank = 0;
19
20 while (@ARGV) {
21     $filename = shift;
22
23     open FILE, "<$filename" or die "Failed to open $filename: $!\n";
24
25     @output = ();
26
27     while (<FILE>) {
28         #
29         #  Clear out trailing whitespace
30         #
31         s/[ \t]+$//;
32
33         #
34         #  And CR's
35         #
36         s/\r//g;
37
38         #
39         #  Suppress multiple blank lines
40         #
41         if (/^\s+$/) {
42             next if ($blank == 1);
43             $blank = 1;
44             push @output, "\n";
45             next;
46         }
47         $blank = 0;
48
49         #
50         #  Remember the vendor
51         #
52         if (/^VENDOR\s+([\w-]+)\s+(\w+)(.*)/) {
53             $name=$1;
54             $len = length $name;
55             if ($len < 32) {
56                 $lenx = 32 - $len;
57                 $lenx += 7;             # round up
58                 $lenx /= 8;
59                 $lenx = int $lenx;
60                 $tabs = "\t" x $lenx;
61             } else {
62                 $tabs = " ";
63             }
64             push @output, "VENDOR\t\t$name$tabs$2$3\n";
65             $vendor = $name;
66             next;
67         }
68
69         #
70         #  Remember if we did begin-vendor.
71         #
72         if (/^BEGIN-VENDOR\s+([\w-]+)/) {
73             $begin_vendor = 1;
74             if (!defined $vendor) {
75                 $vendor = $1;
76             } elsif ($vendor ne $1) {
77                 # do something smart
78             }
79
80             push @output, "BEGIN-VENDOR\t$vendor\n";
81             next;
82         }
83
84         #
85         #  Get attribute.
86         #
87         if (/^ATTRIBUTE\s+([\w-]+)\s+(\w+)\s+(\w+)(.*)/) {
88             $name=$1;
89             $len = length $name;
90             if ($len < 40) {
91                 $lenx = 40 - $len;
92                 $lenx += 7;             # round up
93                 $lenx /= 8;
94                 $lenx = int $lenx;
95                 $tabs = "\t" x $lenx;
96                 if ($tabs eq "") {
97                     $tabs = " ";
98                 }
99             } else {
100                 $tabs = " ";
101             }
102
103             $value = $2;
104             $type = $3;
105             $stuff = $4;
106
107             #
108             #  See if it's old format, with the vendor at the end of
109             #  the line.  If so, make it the new format.
110             #
111             if ($stuff =~ /$vendor/) {
112                 if ($begin_vendor == 0) {
113                     push @output, "BEGIN-VENDOR\t$vendor\n\n";
114                     $begin_vendor = 1;
115                 }
116                 $stuff =~ s/$vendor//;
117                 $stuff =~ s/\s+$//;
118             }
119
120             push @output, "ATTRIBUTE\t$name$tabs$value\t$type$stuff\n";
121             next;
122         }
123
124         #
125         #  Values.
126         #
127         if (/^VALUE\s+([\w-]+)\s+([\w-\/,.]+)\s+(\w+)(.*)/) {
128             $attr=$1;
129             $len = length $attr;
130             if ($len < 32) {
131                 $lenx = 32 - $len;
132                 $lenx += 7;             # round up
133                 $lenx /= 8;
134                 $lenx = int $lenx;
135                 $tabsa = "\t" x $lenx;
136                 if ($tabsa eq "") {
137                     $tabsa = " ";
138                     $len += 1;
139                 } else {
140                     $len -= $len % 8;
141                     $len += 8 * length $tabsa;
142                 }
143             } else {
144                 $tabsa = " ";
145                 $len += 1;
146             }
147
148             #
149             #  For the code below, we assume that the attribute lengths
150             #
151             if ($len < 32) {
152                 $lena = 0;
153             } else {
154                 $lena = $len - 32;
155             }
156
157             $name = $2;
158             $len = length $name;
159             if ($len < 24) {
160                 $lenx = 24 - $lena - $len;
161                 $lenx += 7;             # round up
162                 $lenx /= 8;
163                 $lenx = int $lenx;
164                 $tabsn = "\t" x $lenx;
165                 if ($tabsn eq "") {
166                     $tabsn = " ";
167                 }
168             } else {
169                 $tabsn = " ";
170             }
171
172             push @output, "VALUE\t$attr$tabsa$name$tabsn$3$4\n";
173             next;
174         }
175
176         #
177         #  Remember if we did this.
178         #
179         if (/^END-VENDOR/) {
180             $begin_vendor = 0;
181         }
182
183         #
184         #  Everything else gets dumped out as-is.
185         #
186         push @output, $_;
187     }
188
189 #
190 #  If we changed the format, print the end vendor, too.
191 #
192     if ($begin_vendor) {
193         push @output, "\nEND-VENDOR\t$vendor\n";
194     }
195
196     close FILE;
197
198     open FILE, ">$filename" or die "Failed to open $filename: $!\n";
199
200     print FILE @output;
201
202     close FILE;
203 }