route_types.awk 4.1 KB

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