comparison 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
comparison
equal deleted inserted replaced
1433:3338f135989d 1434:8850d29960aa
1 #!/usr/bin/env perl 1 #!/usr/bin/env perl
2 use strict; 2 use strict;
3 use File::Basename;
3 use Getopt::Std; 4 use Getopt::Std;
5
6 my ($progname) = basename($0);
4 7
5 our ($opt_V, $opt_v); 8 our ($opt_V, $opt_v);
6 9
7 # default to 3GPP 10 # default to Base
8 my ($vendor) = 10415; 11 my ($vendor) = 0;
9 my ($vendor_name) = "3GPP"; 12 my ($vendor_name) = "";
10 13
11 sub convert_must_to_flags($) { 14 sub convert_must_to_flags($) {
12 my ($allmust) = @_; 15 my ($allmust) = @_;
13 my ($mustfields) = ""; 16 my ($mustfields) = "";
14 $mustfields .= "AVP_FLAG_VENDOR |" if ($allmust =~ m/V/); 17 $mustfields .= "AVP_FLAG_VENDOR |" if ($allmust =~ m/V/);
30 return "AVP_TYPE_FLOAT64" if ($type =~ m/Float64/); 33 return "AVP_TYPE_FLOAT64" if ($type =~ m/Float64/);
31 34
32 return "UNKNOWN TYPE: $type"; 35 return "UNKNOWN TYPE: $type";
33 } 36 }
34 37
38
39 my ($comment_width) = 64;
40
41 sub print_header() {
42 printf "\t/*=%s=*/\n", '=' x $comment_width;
43 }
44
45 sub print_comment($) {
46 my ($str) = @_;
47 printf "\t/* %-*s */\n", $comment_width, $str;
48 }
49
35 sub print_insert($$) { 50 sub print_insert($$) {
36 my ($type, $name) = @_; 51 my ($type, $name) = @_;
37 my $avp_type; 52 my $avp_type;
38 53
39 if ($type =~ m/(Grouped|OctetString|Integer32|Integer64|Unsigned32|Unsigned64|Float32|Float64)/) { 54 if ($type =~ m/(Grouped|OctetString|Integer32|Integer64|Unsigned32|Unsigned64|Float32|Float64)/) {
40 $avp_type = "NULL"; 55 $avp_type = "NULL";
41 } elsif ($type =~ m/Enumerated/) { 56 } elsif ($type =~ m/Enumerated/) {
42 print "\t\tstruct dict_object *type;\n"; 57 print "\t\tstruct dict_object\t*type;\n";
43 print "\t\tstruct dict_type_data tdata = { AVP_TYPE_INTEGER32, \"Enumerated(" . ($vendor_name ? "$vendor_name/" : "") ."$name)\", NULL, NULL, NULL };\n"; 58 print "\t\tstruct dict_type_data\t tdata = { AVP_TYPE_INTEGER32, \"Enumerated(" . ($vendor_name ? "$vendor_name/" : "") ."$name)\", NULL, NULL, NULL };\n";
44 # XXX: add enumerated values 59 # XXX: add enumerated values
45 print "\t\tCHECK_dict_new(DICT_TYPE, &tdata, NULL, &type);\n"; 60 print "\t\tCHECK_dict_new(DICT_TYPE, &tdata, NULL, &type);\n";
46 $avp_type = "type"; 61 $avp_type = "type";
47 } else { 62 } else {
48 $avp_type = "${type}_type"; 63 $avp_type = "${type}_type";
50 65
51 print "\t\tCHECK_dict_new(DICT_AVP, &data, $avp_type, NULL);\n"; 66 print "\t\tCHECK_dict_new(DICT_AVP, &data, $avp_type, NULL);\n";
52 } 67 }
53 68
54 sub usage($) { 69 sub usage($) {
55 die("usage: org_to_fd.pl [-V vendor_name -v vendor_code] [file ...]\n"); 70 die(sprintf("usage: %s [-V vendor_name] [-v vendor_code] [file ...]\n", $progname));
56 exit(@_); 71 exit(@_);
57 } 72 }
58 73
59 getopts("V:v:") || usage(1); 74 getopts("V:v:") || usage(1);
60 75
61 if (defined($opt_v)) { 76 if (defined($opt_v)) {
62 $vendor = $opt_v; 77 $vendor = $opt_v;
63 if (!defined($opt_V)) { 78 if (!defined($opt_V)) {
64 usage(1); 79 usage(1);
65 } 80 }
66 $vendor_name = $opt_V; 81 $vendor_name = $opt_V;
67 } 82 }
68 83
69 print "\t/* The following is created automatically. Do not modify. */\n"; 84 print_header();
70 print "\t/* Changes will be lost during the next update. Modify the source org file instead. */\n\n"; 85 print_comment("Start of generated data.");
86 print_comment("");
87 print_comment("The following is created automatically with:");
88 print_comment(sprintf(" org_to_fd.pl -V '%s' -v %s", $vendor_name, $vendor));
89 print_comment("Changes will be lost during the next update.");
90 print_comment("Do not modify; modify the source .org file instead.");
91 print_header();
92 print "\n";
71 93
72 while (<>) { 94 while (<>) {
73 my ($dummy, $name, $code, $section, $type, $must, $may, $shouldnot, $mustnot, $encr) = split /\s*\|\s*/; 95 my ($dummy, $name, $code, $section, $type, $must, $may, $shouldnot, $mustnot, $encr) = split /\s*\|\s*/;
74 96
75 next if ($name =~ m/Attribute Name/); 97 next if ($name =~ m/Attribute Name/);
76 if ($name =~ m/# (.*)/) { 98 if ($name =~ m/# (.*)/) {
77 printf "\t/* %-60s */\n", $1; 99 print_comment($1);
78 next; 100 next;
79 } 101 }
80 102
81 if ($name =~ m/\s/) { 103 if ($name =~ m/\s/) {
82 die(sprintf("name '%s' contains space", $name)); 104 die(sprintf("name '%s' contains space", $name));
83 } 105 }
84 106
85 print "\t/* $name */\n\t{\n\t\tstruct dict_avp_data data = {\n"; 107 my ($desc) = $name;
108 $desc .= ", " . $type;
109 $desc .= ", code " . $code;
110 $desc .= ", section " . $section if $section != "";
111 print_comment($desc);
112 print "\t{\n";
113 print "\t\tstruct dict_avp_data data = {\n";
86 print "\t\t\t$code,\t/* Code */\n"; 114 print "\t\t\t$code,\t/* Code */\n";
87 print "\t\t\t$vendor,\t/* Vendor */\n"; 115 print "\t\t\t$vendor,\t/* Vendor */\n";
88 print "\t\t\t\"$name\",\t/* Name */\n"; 116 print "\t\t\t\"$name\",\t/* Name */\n";
89 print "\t\t\t" . convert_must_to_flags("$must, $mustnot") . ",\t/* Fixed flags */\n"; 117 print "\t\t\t" . convert_must_to_flags("$must, $mustnot") . ",\t/* Fixed flags */\n";
90 print "\t\t\t" . convert_must_to_flags("$must") . ",\t/* Fixed flag values */\n"; 118 print "\t\t\t" . convert_must_to_flags("$must") . ",\t/* Fixed flag values */\n";
91 print "\t\t\t" . base_type($type) . "\t/* base type of data */\n"; 119 print "\t\t\t" . base_type($type) . "\t/* base type of data */\n";
92 print "\t\t};\n"; 120 print "\t\t};\n";
93 print_insert($type, $name); 121 print_insert($type, $name);
94 print "\t};\n\n"; 122 print "\t};\n\n";
95 } 123 }
124
125 print_header();
126 print_comment("End of generated data.");
127 print_header();
"Welcome to our mercurial repository"