]> git.proxmox.com Git - mirror_frr.git/blob - lib/route_types.awk
[lib] Centralise Zserv route type information, auto-generate redist strings
[mirror_frr.git] / lib / route_types.awk
1 # $Id$
2 #
3 # Scan a file of route-type definitions (see eg route_types.txt) and
4 # generate a corresponding header file with:
5 #
6 # - enum of Zserv route-types
7 # - redistribute strings for the various Quagga daemons
8 #
9 # See route_types.txt for the format.
10 #
11 #
12
13 BEGIN {
14 FS="[,]";
15
16 # globals
17 exitret = 0;
18 tcount = 0;
19
20 # 'bare' redistribute specifier, <1-255>, help string.
21 redist_bare_help = "Numeric Zserv route type";
22
23 # formats for output
24 redist_def_fmt = "#define QUAGGA_REDIST_STR_%s \\\n";
25 redist_str_fmt = "\"(%s<1-255>)\"\n";
26 redist_help_def_fmt = "#define QUAGGA_REDIST_HELP_STR_%s";
27 redist_help_str_fmt = " \\\n \"%s\\n\"";
28
29 # header
30 header = "/* Auto-generated from route_types.txt by " ARGV[0] ". */\n";
31 header = header "/* Do not edit! */\n";
32 header = header "\n#ifndef _QUAGGA_ROUTE_TYPES_H\n";
33 header = header "#define _QUAGGA_ROUTE_TYPES_H\n";
34 footer = "#endif /* _QUAGGA_ROUTE_TYPES_H */\n";
35 printf ("%s\n", header);
36 }
37
38 # Chomp comment lines
39 ($0 ~ /^#/) {
40 next;
41 }
42
43 # get rid of the commas, leading/trailling whitespace and
44 # quotes
45 {
46 for (i = 1; i <= NF; i++) {
47 #print "before:" $i;
48 $i = gensub(/^[[:blank:]]*(.*)[,]*.*/, "\\1", "g",$i);
49 $i = gensub(/^["](.*)["]$/, "\\1", "g", $i);
50 #print "after :" $i;
51 }
52 }
53
54 # 7 field format:
55 # type cname daemon C 4 6 short help
56 (NF >= 7) {
57 #print "7", $1, $0;
58
59 if ($1 in types) {
60 print "error: attempt to redefine", $1;
61 exitret = 1;
62 exit exitret;
63 }
64
65 typesbynum[tcount] = $1;
66 types[$1,"num"] = tcount++;
67 types[$1,"cname"] = $2;
68 types[$1,"daemon"] = $3;
69 types[$1,"C"] = $4;
70 types[$1,"4"] = strtonum($5);
71 types[$1,"6"] = strtonum($6);
72 types[$1,"shelp"] = $7;
73
74 #print "num :", types[$1,"num"]
75 #print "cname :", types[$1,"cname"]
76 #print "daemon:", types[$1,"daemon"];
77 #print "char :", types[$1,"C"];
78 };
79
80 # 2 field: type "long description"
81 (NF == 2) {
82 #print "2", $1, $2;
83
84 if (!(($1 SUBSEP "num") in types)) {
85 print "error: type", $1, "must be defined before help str";
86 exitret = 2;
87 exit exitret;
88 }
89
90 types[$1,"lhelp"] = $2;
91 }
92
93 END {
94 if (exitret)
95 exit exitret;
96
97 # The enums
98 # not yet...
99 #printf("enum\n{\n");
100 #for (i = 0; i < tcount; i++) {
101 # type = typesbynum[i];
102 # if (type != "" && types[type,"num"] == i)
103 # printf (" %s,\n", type);
104 #}
105 #printf (" ZEBRA_ROUTE_MAX,\n};\n\n");
106
107 # the redistribute defines
108 for (i = 0; i < tcount; i++) {
109 type = typesbynum[i];
110
111 # must be a type, and must cross-check against recorded type
112 if (type == "" || types[type,"num"] != i)
113 continue;
114
115 # ignore route types that can't be redistributed
116 if (!(types[type,"4"] || types[type,"6"]))
117 continue;
118
119 # must have a daemon name
120 if (!((type SUBSEP "daemon") in types))
121 continue;
122 if (!(daemon = types[type,"daemon"]))
123 continue;
124
125 # might have done this daemon already?
126 if (daemon in seen_daemons)
127 continue;
128
129 cname = types[type,"cname"];
130 all = all "|" cname;
131 rstr = "";
132 hstr = "";
133
134 # add it to the others
135 for (j = 0; j < tcount; j++) {
136 # ignore self
137 if (i == j)
138 continue;
139
140 type2 = typesbynum[j];
141
142 # type2 must be valid, and self-check.
143 if (type2 == "" || types[type2,"num"] != j)
144 continue;
145
146 # ignore different route types for the same daemon
147 # (eg system/kernel/connected)
148 if (types[type2,"daemon"] == daemon)
149 continue;
150
151 if ((types[type2,"4"] && types[type,"4"]) \
152 || (types[type2,"6"] && types[type,"6"])) {
153
154 rstr = rstr types[type2,"cname"] "|";
155
156 if ((type2 SUBSEP "lhelp") in types)
157 hstr2 = types[type2,"lhelp"];
158 else if ((type2 SUBSEP "shelp") in types)
159 hstr2 = types[type2,"shelp"];
160 else
161 hstr2 = types[type2,"cname"];
162
163 hstr = hstr sprintf(redist_help_str_fmt, hstr2);
164 }
165 }
166
167 # dont double-process daemons.
168 seen_daemons[daemon] = 1;
169
170 printf("/* %s */\n", daemon);
171 printf(redist_def_fmt, toupper(daemon));
172 printf(redist_str_fmt, rstr);
173 printf(redist_help_def_fmt, toupper(daemon));
174 printf("%s", hstr);
175 printf(redist_help_str_fmt, redist_bare_help);
176 print("\n");
177
178 }
179
180 #printf("#define QUAGGA_REDIST_STR_ALL %s\n",all);
181
182 # for (i = 0; i < lcount; i++) {
183 # if (mlists[i] != "")
184 # printf (mlistformat "\n", mlists[i]);
185 # }
186 printf (footer);
187 }