2 ######################################################################
3 # Copyright (c) 2011, Network RADIUS SARL
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the <organization> nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ######################################################################
29 # Converts dictionaries to C structures. Does not yet do "VALUE"s.
31 # Usage: ./convert.pl dictionary ...
33 # Reads input dictionaries, and outputs "radius.h" and "dictionaries.c"
40 # Read all of the dictionaries
48 # For speed, the dictionary data structures have the first 256
49 # attributes at fixed offsets in the array. If the user didn't
50 # define them, then we set them here to be "raw" or unknown.
52 foreach $attr_val (0..255) {
53 next if defined $attributes{$attr_val};
55 $attributes{$attr_val}{'raw'} = 1;
58 if (scalar keys %attributes == 0) {
59 die "No attributes were defined\n";
63 open DICT, ">dictionaries.c" or die "Failed creating dictionaries.c: $!\n";
66 # Print out the data structues for the vendors.
68 if (scalar keys %vendor > 0) {
69 print DICT "const DICT_VENDOR nr_dict_vendors[] = {\n";
70 foreach $v (sort keys %vendor) {
72 print DICT " .name = \"", $v, "\", \n";
73 print DICT " .vendor = ", $vendor{$v}{'pec'}, ", \n";
74 print DICT " .type = ", $vendor{$v}{'type'}, ",\n";
75 print DICT " .length = ", $vendor{$v}{'length'}, ",\n";
78 print DICT "\n { .name = NULL, }\n";
83 $vendor{""}{'pec'} = 0;
86 # Print DICT out the attributes sorted by number.
90 print DICT "const DICT_ATTR nr_dict_attrs[] = {\n";
91 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
92 print DICT " { /* $offset */ \n";
94 if (defined $attributes{$attr_val}{'raw'}) {
95 print DICT " .name = NULL, \n";
97 print DICT " .name = \"", $attributes{$attr_val}{'name'}, "\", \n";
98 if ($attributes{$attr_val}{'vendor'}) {
99 print DICT " .vendor = ", $attributes{$attr_val}{'vendor'}, ", \n";
102 print DICT " .attr = ", $attributes{$attr_val}{'value'}, ", \n";
103 print DICT " .type = ", $attributes{$attr_val}{'type'}, ", \n";
105 if ($attributes{$attr_val}{'flags'}) {
106 print DICT " .flags = {\n";
107 foreach $flag (keys %{$attributes{$attr_val}{'flags'}}) {
108 print DICT " .$flag = $attributes{$attr_val}{'flags'}{$flag},\n";
116 $attributes{$attr_val}{'offset'} = $offset++;
123 print DICT "const int nr_dict_num_attrs = ", $offset - 1, ";\n\n";
124 print DICT "const int nr_dict_num_names = ", $num_names - 1, ";\n\n";
127 print DICT "const DICT_ATTR *nr_dict_attr_names[] = {\n";
128 foreach $attr_val (sort {lc($attributes{$a}{'name'}) cmp lc($attributes{$b}{'name'})} keys %attributes) {
129 next if (defined $attributes{$attr_val}{'raw'});
131 print DICT " &nr_dict_attrs[", $attributes{$attr_val}{'offset'}, "], /* ", $attributes{$attr_val}{'name'}, " */\n";
137 open HDR, ">radius.h" or die "Failed creating radius.c: $!\n";
139 print HDR "/* Automatically generated file. Do not edit */\n\n";
141 foreach $v (sort keys %vendor) {
145 $name =~ tr/a-z/A-Z/; # uppercase
146 $name =~ tr/A-Z0-9/_/c; # any ELSE becomes _
148 print HDR "#define VENDORPEC_", $name, " ", $vendor{$v}{'pec'}, "\n";
153 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
154 next if (defined $attributes{$attr_val}{'raw'});
156 if ($attributes{$attr_val}{'vendor'} != $begin_vendor) {
157 print HDR "\n/* ", $vendorpec{$attributes{$attr_val}{'vendor'}}, " */\n";
158 $begin_vendor = $attributes{$attr_val}{'vendor'};
161 $name = $attributes{$attr_val}{'name'};
162 $name =~ tr/a-z/A-Z/;
163 $name =~ tr/A-Z0-9/_/c;
165 print HDR "#define PW_", $name, " ", $attributes{$attr_val}{'value'}, "\n";
169 print HDR "/* Fixed offsets to dictionary definitions of attributes */\n";
170 foreach $attr_val (sort {$a <=> $b} keys %attributes) {
171 next if (defined $attributes{$attr_val}{'raw'});
173 $name = $attributes{$attr_val}{'name'};
174 $name =~ tr/a-z/A-Z/;
177 print HDR "#define NR_DA_$name (&nr_dict_attrs[$attributes{$attr_val}{'offset'}])\n";
180 print HDR "/* Automatically generated file. Do not edit */\n";