annotate contrib/tools/org_to_fd.pl @ 1434:8850d29960aa

org_to_fd.pl: improve formatting of generated data - Add more explicit comment blocks for "Start of generated data" and "End of generated data", including the invocation of org_to_fd.pl. - Change the default vendor to Base: code 0, name "". - Add Type, Code, and Section (if present) to the AVP comment. - Consistently use \t to ensure tab output. - Provide functions to format comment blocks.
author Luke Mewburn <luke@mewburn.net>
date Wed, 19 Feb 2020 18:05:01 +1100
parents 3338f135989d
children 0a80278df21f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
1 #!/usr/bin/env perl
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
2 use strict;
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
3 use File::Basename;
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
4 use Getopt::Std;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
5
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
6 my ($progname) = basename($0);
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
7
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
8 our ($opt_V, $opt_v);
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
9
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
10 # default to Base
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
11 my ($vendor) = 0;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
12 my ($vendor_name) = "";
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
13
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
14 sub convert_must_to_flags($) {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
15 my ($allmust) = @_;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
16 my ($mustfields) = "";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
17 $mustfields .= "AVP_FLAG_VENDOR |" if ($allmust =~ m/V/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
18 $mustfields .= "AVP_FLAG_MANDATORY |" if ($allmust =~ m/M/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
19 $mustfields =~ s/ \|$//;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
20 return $mustfields;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
21 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
22
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
23 sub base_type($) {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
24 my ($type) = @_;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
25
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
26 return "AVP_TYPE_GROUPED" if ($type =~ m/Grouped/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
27 return "AVP_TYPE_OCTETSTRING" if ($type =~ m/(Address|DiameterIdentity|DiameterURI|OctetString|IPFilterRule|Time|UTF8String)/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
28 return "AVP_TYPE_INTEGER32" if ($type =~ m/Enumerated|Integer32/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
29 return "AVP_TYPE_INTEGER64" if ($type =~ m/Integer64/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
30 return "AVP_TYPE_UNSIGNED32" if ($type =~ m/Unsigned32/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
31 return "AVP_TYPE_UNSIGNED64" if ($type =~ m/Unsigned64/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
32 return "AVP_TYPE_FLOAT32" if ($type =~ m/Float32/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
33 return "AVP_TYPE_FLOAT64" if ($type =~ m/Float64/);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
34
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
35 return "UNKNOWN TYPE: $type";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
36 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
37
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
38
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
39 my ($comment_width) = 64;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
40
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
41 sub print_header() {
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
42 printf "\t/*=%s=*/\n", '=' x $comment_width;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
43 }
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
44
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
45 sub print_comment($) {
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
46 my ($str) = @_;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
47 printf "\t/* %-*s */\n", $comment_width, $str;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
48 }
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
49
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
50 sub print_insert($$) {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
51 my ($type, $name) = @_;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
52 my $avp_type;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
53
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
54 if ($type =~ m/(Grouped|OctetString|Integer32|Integer64|Unsigned32|Unsigned64|Float32|Float64)/) {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
55 $avp_type = "NULL";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
56 } elsif ($type =~ m/Enumerated/) {
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
57 print "\t\tstruct dict_object\t*type;\n";
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
58 print "\t\tstruct dict_type_data\t tdata = { AVP_TYPE_INTEGER32, \"Enumerated(" . ($vendor_name ? "$vendor_name/" : "") ."$name)\", NULL, NULL, NULL };\n";
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
59 # XXX: add enumerated values
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
60 print "\t\tCHECK_dict_new(DICT_TYPE, &tdata, NULL, &type);\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
61 $avp_type = "type";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
62 } else {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
63 $avp_type = "${type}_type";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
64 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
65
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
66 print "\t\tCHECK_dict_new(DICT_AVP, &data, $avp_type, NULL);\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
67 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
68
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
69 sub usage($) {
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
70 die(sprintf("usage: %s [-V vendor_name] [-v vendor_code] [file ...]\n", $progname));
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
71 exit(@_);
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
72 }
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
73
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
74 getopts("V:v:") || usage(1);
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
75
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
76 if (defined($opt_v)) {
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
77 $vendor = $opt_v;
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
78 if (!defined($opt_V)) {
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
79 usage(1);
960
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
80 }
f39fa6cd86e0 Include vendor name in enumerated type name.
Thomas Klausner <tk@giga.or.at>
parents: 953
diff changeset
81 $vendor_name = $opt_V;
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
82 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
83
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
84 print_header();
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
85 print_comment("Start of generated data.");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
86 print_comment("");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
87 print_comment("The following is created automatically with:");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
88 print_comment(sprintf(" org_to_fd.pl -V '%s' -v %s", $vendor_name, $vendor));
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
89 print_comment("Changes will be lost during the next update.");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
90 print_comment("Do not modify; modify the source .org file instead.");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
91 print_header();
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
92 print "\n";
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
93
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
94 while (<>) {
1433
3338f135989d org_to_fd.pl: prevent spaces in AVP names
Luke Mewburn <luke@mewburn.net>
parents: 1324
diff changeset
95 my ($dummy, $name, $code, $section, $type, $must, $may, $shouldnot, $mustnot, $encr) = split /\s*\|\s*/;
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
96
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
97 next if ($name =~ m/Attribute Name/);
1433
3338f135989d org_to_fd.pl: prevent spaces in AVP names
Luke Mewburn <luke@mewburn.net>
parents: 1324
diff changeset
98 if ($name =~ m/# (.*)/) {
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
99 print_comment($1);
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
100 next;
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
101 }
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
102
1433
3338f135989d org_to_fd.pl: prevent spaces in AVP names
Luke Mewburn <luke@mewburn.net>
parents: 1324
diff changeset
103 if ($name =~ m/\s/) {
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
104 die(sprintf("name '%s' contains space", $name));
1433
3338f135989d org_to_fd.pl: prevent spaces in AVP names
Luke Mewburn <luke@mewburn.net>
parents: 1324
diff changeset
105 }
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
106
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
107 my ($desc) = $name;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
108 $desc .= ", " . $type;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
109 $desc .= ", code " . $code;
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
110 $desc .= ", section " . $section if $section != "";
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
111 print_comment($desc);
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
112 print "\t{\n";
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
113 print "\t\tstruct dict_avp_data data = {\n";
953
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
114 print "\t\t\t$code,\t/* Code */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
115 print "\t\t\t$vendor,\t/* Vendor */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
116 print "\t\t\t\"$name\",\t/* Name */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
117 print "\t\t\t" . convert_must_to_flags("$must, $mustnot") . ",\t/* Fixed flags */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
118 print "\t\t\t" . convert_must_to_flags("$must") . ",\t/* Fixed flag values */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
119 print "\t\t\t" . base_type($type) . "\t/* base type of data */\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
120 print "\t\t};\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
121 print_insert($type, $name);
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
122 print "\t};\n\n";
d35276067342 Add tools that create freeDiameter C code from org tables containing the information.
Thomas Klausner <tk@giga.or.at>
parents:
diff changeset
123 }
1434
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
124
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
125 print_header();
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
126 print_comment("End of generated data.");
8850d29960aa org_to_fd.pl: improve formatting of generated data
Luke Mewburn <luke@mewburn.net>
parents: 1433
diff changeset
127 print_header();
"Welcome to our mercurial repository"