route_types.awk 4.1 KB

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