memtypes.awk 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ###
  2. # Copyright (C) Paul Jakma 2005
  3. #
  4. # This file is part of Quagga.
  5. #
  6. # Quagga is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the
  8. # Free Software Foundation; either version 2, or (at your option) any
  9. # later version.
  10. #
  11. # Quagga is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Quagga; see the file COPYING. If not, write to the Free
  18. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. # 02111-1307, USA.
  20. ###
  21. #
  22. # Scan a file of memory definitions (see eg memtypes.c) and generate
  23. # a corresponding header file with an enum of the MTYPE's and declarations
  24. # for the struct memory_list arrays
  25. #
  26. # struct memory_list's must be declared as:
  27. # '\nstruct memory_list memory_list_<name>[] .....'
  28. #
  29. # Each MTYPE_ within the definition must the second token on the line,
  30. # tokens being delineated by whitespace. It may only consist of the set of
  31. # characters [[:upper:]_[:digit:]]. Eg:
  32. #
  33. # '\n { MTYPE_AWESOME_IPV8 , "Amazing new protocol, says genius" {}..boo'
  34. #
  35. # We try to ignore lines whose first token is /* or *, ie C comment lines.
  36. # So the following should work fine:
  37. #
  38. # '/* This is the best memory_list ever!
  39. # ' * It's got all my MTYPE's */
  40. # '
  41. # 'struct memory_list memory_list_my_amazing_mlist[] = =
  42. # '{
  43. # ' { MTYPE_DONGLE, "Dongle widget" }
  44. # ' { MTYPE_FROB, "Frobulator" },
  45. # '{ MTYPE_WIPPLE, "Wipple combombulator"}
  46. # '}}}
  47. #
  48. # Even if it isn't quite a valid C declaration.
  49. #
  50. BEGIN {
  51. mlistregex = "memory_list_(.*)\\[\\]";
  52. mtyperegex = "^(MTYPE_[[:upper:]_[:digit:]]+).*";
  53. header = "/* Auto-generated from memtypes.c by " ARGV[0] ". */\n";
  54. header = header "/* Do not edit! */\n";
  55. header = header "\n#ifndef _QUAGGA_MEMTYPES_H\n";
  56. header = header "#define _QUAGGA_MEMTYPES_H\n";
  57. footer = "\n#endif /* _QUAGGA_MEMTYPES_H */\n\n";
  58. mlistformat = "extern struct memory_list memory_list_%s[];";
  59. printf ("%s\n", header);
  60. }
  61. # catch lines beginning with 'struct memory list ' and try snag the
  62. # memory_list name. Has to be 3rd field.
  63. ($0 ~ /^struct memory_list /) && (NF >= 3) {
  64. mlists[lcount++] = gensub(mlistregex, "\\1", "g",$3);
  65. }
  66. # snag the MTYPE, it must self-standing and the second field,
  67. # though we do manage to tolerate the , C seperator being appended
  68. ($1 !~ /^\/?\*/) && ($2 ~ /^MTYPE_/) {
  69. mtype[tcount++] = gensub(mtyperegex, "\\1", "g", $2);
  70. }
  71. END {
  72. printf("enum\n{\n MTYPE_TMP = 1,\n");
  73. for (i = 0; i < tcount; i++) {
  74. if (mtype[i] != "" && mtype[i] != "MTYPE_TMP")
  75. printf (" %s,\n", mtype[i]);
  76. }
  77. printf (" MTYPE_MAX,\n};\n\n");
  78. for (i = 0; i < lcount; i++) {
  79. if (mlists[i] != "")
  80. printf (mlistformat "\n", mlists[i]);
  81. }
  82. printf (footer);
  83. }