memtypes.awk 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # $Id: memtypes.awk,v 1.4 2006/03/30 14:30:19 paul Exp $
  2. #
  3. # Scan a file of memory definitions (see eg memtypes.c) and generate
  4. # a corresponding header file with an enum of the MTYPE's and declarations
  5. # for the struct memory_list arrays
  6. #
  7. # struct memory_list's must be declared as:
  8. # '\nstruct memory_list memory_list_<name>[] .....'
  9. #
  10. # Each MTYPE_ within the definition must the second token on the line,
  11. # tokens being delineated by whitespace. It may only consist of the set of
  12. # characters [[:upper:]_[:digit:]]. Eg:
  13. #
  14. # '\n { MTYPE_AWESOME_IPV8 , "Amazing new protocol, says genius" {}..boo'
  15. #
  16. # We try to ignore lines whose first token is /* or *, ie C comment lines.
  17. # So the following should work fine:
  18. #
  19. # '/* This is the best memory_list ever!
  20. # ' * It's got all my MTYPE's */
  21. # '
  22. # 'struct memory_list memory_list_my_amazing_mlist[] = =
  23. # '{
  24. # ' { MTYPE_DONGLE, "Dongle widget" }
  25. # ' { MTYPE_FROB, "Frobulator" },
  26. # '{ MTYPE_WIPPLE, "Wipple combombulator"}
  27. # '}}}
  28. #
  29. # Even if it isn't quite a valid C declaration.
  30. #
  31. BEGIN {
  32. mlistregex = "memory_list_(.*)\\[\\]";
  33. mtyperegex = "^(MTYPE_[[:upper:]_[:digit:]]+).*";
  34. header = "/* Auto-generated from memtypes.c by " ARGV[0] ". */\n";
  35. header = header "/* Do not edit! */\n";
  36. header = header "\n#ifndef _QUAGGA_MEMTYPES_H\n";
  37. header = header "#define _QUAGGA_MEMTYPES_H\n";
  38. footer = "\n#endif /* _QUAGGA_MEMTYPES_H */\n\n";
  39. mlistformat = "extern struct memory_list memory_list_%s[];";
  40. printf ("%s\n", header);
  41. }
  42. # catch lines beginning with 'struct memory list ' and try snag the
  43. # memory_list name. Has to be 3rd field.
  44. ($0 ~ /^struct memory_list /) && (NF >= 3) {
  45. mlists[lcount++] = gensub(mlistregex, "\\1", "g",$3);
  46. }
  47. # snag the MTYPE, it must self-standing and the second field,
  48. # though we do manage to tolerate the , C seperator being appended
  49. ($1 !~ /^\/?\*/) && ($2 ~ /^MTYPE_/) {
  50. mtype[tcount++] = gensub(mtyperegex, "\\1", "g", $2);
  51. }
  52. END {
  53. printf("enum\n{\n MTYPE_TMP = 1,\n");
  54. for (i = 0; i < tcount; i++) {
  55. if (mtype[i] != "" && mtype[i] != "MTYPE_TMP")
  56. printf (" %s,\n", mtype[i]);
  57. }
  58. printf (" MTYPE_MAX,\n};\n\n");
  59. for (i = 0; i < lcount; i++) {
  60. if (mlists[i] != "")
  61. printf (mlistformat "\n", mlists[i]);
  62. }
  63. printf (footer);
  64. }