Don't call free on talloc'ed channel bindings packet
[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 ######################################################################
15 #
16 #    This program is free software; you can redistribute it and/or modify
17 #    it under the terms of the GNU General Public License as published by
18 #    the Free Software Foundation; either version 2 of the License, or
19 #    (at your option) any later version.
20 #
21 #    This program is distributed in the hope that it will be useful,
22 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 #    GNU General Public License for more details.
25 #
26 #    You should have received a copy of the GNU General Public License
27 #    along with this program; if not, write to the Free Software
28 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #
30 #    Copyright (C) 2010 Alan DeKok <aland@freeradius.org>
31 #
32 ######################################################################
33 #
34 #  $Id$
35 #
36
37 $begin_vendor = 0;
38 $blank = 0;
39
40 while (@ARGV) {
41     $filename = shift;
42
43     open FILE, "<$filename" or die "Failed to open $filename: $!\n";
44
45     @output = ();
46
47     while (<FILE>) {
48         #
49         #  Clear out trailing whitespace
50         #
51         s/[ \t]+$//;
52
53         #
54         #  And CR's
55         #
56         s/\r//g;
57
58         #
59         #  Suppress multiple blank lines
60         #
61         if (/^\s+$/) {
62             next if ($blank == 1);
63             $blank = 1;
64             push @output, "\n";
65             next;
66         }
67         $blank = 0;
68
69         s/\s*$/\n/;
70
71         #
72         #  Remember the vendor
73         #
74         if (/^VENDOR\s+([\w-]+)\s+(\w+)(.*)/) {
75             $name=$1;
76             $len = length $name;
77             if ($len < 32) {
78                 $lenx = 32 - $len;
79                 $lenx += 7;             # round up
80                 $lenx /= 8;
81                 $lenx = int $lenx;
82                 $tabs = "\t" x $lenx;
83             } else {
84                 $tabs = " ";
85             }
86             push @output, "VENDOR\t\t$name$tabs$2$3\n";
87             $vendor = $name;
88             next;
89         }
90
91         #
92         #  Remember if we did begin-vendor.
93         #
94         if (/^BEGIN-VENDOR\s+([\w-]+)/) {
95             $begin_vendor = 1;
96             if (!defined $vendor) {
97                 $vendor = $1;
98             } elsif ($vendor ne $1) {
99                 # do something smart
100             }
101
102             push @output, "BEGIN-VENDOR\t$vendor\n";
103             next;
104         }
105
106         #
107         #  Get attribute.
108         #
109         if (/^ATTRIBUTE\s+([\w-]+)\s+([\w.]+)\s+(\w+)(.*)/) {
110             $name=$1;
111             $len = length $name;
112             if ($len < 40) {
113                 $lenx = 40 - $len;
114                 $lenx += 7;             # round up
115                 $lenx /= 8;
116                 $lenx = int $lenx;
117                 $tabs = "\t" x $lenx;
118                 if ($tabs eq "") {
119                     $tabs = " ";
120                 }
121             } else {
122                 $tabs = " ";
123             }
124
125             $value = $2;
126             $type = $3;
127             $stuff = $4;
128
129             #
130             #  See if it's old format, with the vendor at the end of
131             #  the line.  If so, make it the new format.
132             #
133             if ($stuff =~ /$vendor/) {
134                 if ($begin_vendor == 0) {
135                     push @output, "BEGIN-VENDOR\t$vendor\n\n";
136                     $begin_vendor = 1;
137                 }
138                 $stuff =~ s/$vendor//;
139                 $stuff =~ s/\s+$//;
140             }
141
142             push @output, "ATTRIBUTE\t$name$tabs$value\t$type$stuff\n";
143             next;
144         }
145
146         #
147         #  Values.
148         #
149         if (/^VALUE\s+([\w-]+)\s+([\w-\/,.]+)\s+(\w+)(.*)/) {
150             $attr=$1;
151             $len = length $attr;
152             if ($len < 32) {
153                 $lenx = 32 - $len;
154                 $lenx += 7;             # round up
155                 $lenx /= 8;
156                 $lenx = int $lenx;
157                 $tabsa = "\t" x $lenx;
158                 if ($tabsa eq "") {
159                     $tabsa = " ";
160                     $len += 1;
161                 } else {
162                     $len -= $len % 8;
163                     $len += 8 * length $tabsa;
164                 }
165             } else {
166                 $tabsa = " ";
167                 $len += 1;
168             }
169
170             #
171             #  For the code below, we assume that the attribute lengths
172             #
173             if ($len < 32) {
174                 $lena = 0;
175             } else {
176                 $lena = $len - 32;
177             }
178
179             $name = $2;
180             $len = length $name;
181             if ($len < 24) {
182                 $lenx = 24 - $lena - $len;
183                 $lenx += 7;             # round up
184                 $lenx /= 8;
185                 $lenx = int $lenx;
186                 $tabsn = "\t" x $lenx;
187                 if ($tabsn eq "") {
188                     $tabsn = " ";
189                 }
190             } else {
191                 $tabsn = " ";
192             }
193
194             push @output, "VALUE\t$attr$tabsa$name$tabsn$3$4\n";
195             next;
196         }
197
198         #
199         #  Remember if we did this.
200         #
201         if (/^END-VENDOR/) {
202             $begin_vendor = 0;
203         }
204
205         #
206         #  Everything else gets dumped out as-is.
207         #
208         push @output, $_;
209     }
210
211 #
212 #  If we changed the format, print the end vendor, too.
213 #
214     if ($begin_vendor) {
215         push @output, "\nEND-VENDOR\t$vendor\n";
216     }
217
218     close FILE;
219
220     open FILE, ">$filename" or die "Failed to open $filename: $!\n";
221
222     print FILE @output;
223
224     close FILE;
225 }