Be a little nicer
[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 (<>) {
21     #
22     #  Clear out trailing whitespace
23     #
24     s/[ \t]+$//;
25
26     #
27     #  And CR's
28     #
29     s/\r//g;
30
31     #
32     #  Suppress multiple blank lines
33     #
34     if (/^\s+$/) {
35         next if ($blank == 1);
36         $blank = 1;
37         print "\n";
38         next;
39     }
40     $blank = 0;
41
42     #
43     #  Remember the vendor
44     #
45     if (/^VENDOR\s+([\w-]+)\s+(\w+)(.*)/) {
46         $name=$1;
47         $len = length $name;
48         if ($len < 32) {
49             $lenx = 32 - $len;
50             $lenx += 7;         # round up
51             $lenx /= 8;
52             $lenx = int $lenx;
53             $tabs = "\t" x $lenx;
54         } else {
55             $tabs = " ";
56         }
57         print "VENDOR\t\t$name$tabs$2$3\n";
58         $vendor = $name;
59         next;
60     }
61
62     #  
63     #  Remember if we did begin-vendor.
64     #
65     if (/^BEGIN-VENDOR\s+([\w-]+)/) {
66         $begin_vendor = 1;
67         print "BEGIN-VENDOR\t$vendor\n";
68         next;
69     }
70
71     #
72     #  Get attribute.
73     #
74     if (/^ATTRIBUTE\s+([\w-]+)\s+(\w+)\s+(\w+)(.*)/) {
75         $name=$1;
76         $len = length $name;
77         if ($len < 40) {
78             $lenx = 40 - $len;
79             $lenx += 7;         # round up
80             $lenx /= 8;
81             $lenx = int $lenx;
82             $tabs = "\t" x $lenx;
83             if ($tabs eq "") {
84                 $tabs = " ";
85             }
86         } else {
87             $tabs = " ";
88         }
89
90         $value = $2;
91         $type = $3;
92         $stuff = $4;
93
94         #
95         #  See if it's old format, with the vendor at the end of
96         #  the line.  If so, make it the new format.
97         #
98         if ($stuff =~ /$vendor/) {
99             if ($begin_vendor == 0) {
100                 print "BEGIN-VENDOR\t$vendor\n\n";
101                 $begin_vendor = 1;
102             }
103             $stuff =~ s/$vendor//;
104             $stuff =~ s/\s+$//;
105         }
106
107         print "ATTRIBUTE\t$name$tabs$value\t$type$stuff\n";
108         next;
109     }
110
111     #
112     #  Values.
113     #
114     if (/^VALUE\s+([\w-]+)\s+([\w-]+)\s+(\w+)(.*)/) {
115         $attr=$1;
116         $len = length $attr;
117         if ($len < 32) {
118             $lenx = 32 - $len;
119             $lenx += 7;         # round up
120             $lenx /= 8;
121             $lenx = int $lenx;
122             $tabsa = "\t" x $lenx;
123             if ($tabsa eq "") {
124                 $tabsa = " ";
125                 $len += 1;
126             } else {
127                 $len -= $len % 8;
128                 $len += 8 * length $tabsa;
129             }
130         } else {
131             $tabsa = " ";
132             $len += 1;
133         }
134
135         #
136         #  For the code below, we assume that the attribute lengths
137         #
138         if ($len < 32) {
139             $lena = 0;
140         } else {
141             $lena = $len - 32;
142         }
143
144         $name = $2;
145         $len = length $name;
146         if ($len < 24) {
147             $lenx = 24 - $lena - $len;
148             $lenx += 7;         # round up
149             $lenx /= 8;
150             $lenx = int $lenx;
151             $tabsn = "\t" x $lenx;
152             if ($tabsn eq "") {
153                 $tabsn = " ";
154             }
155         } else {
156             $tabsn = " ";
157         }
158
159         print "VALUE\t$attr$tabsa$name$tabsn$3$4\n";
160         next;
161     }
162
163     #
164     #  Remember if we did this.
165     #
166     if (/^END-VENDOR/) {
167         $begin_vendor = 0;
168     }
169
170     #
171     #  Everything else gets dumped out as-is.
172     #
173     print;
174 }
175
176 #
177 #  If we changed the format, print the end vendor, too.
178 #
179 if ($begin_vendor) {
180     print "\nEND-VENDOR\t$vendor\n";
181 }