Paul Jakma | d4ce4f6 | 2006-03-30 14:30:19 +0000 | [diff] [blame] | 1 | # $Id: memtypes.awk,v 1.4 2006/03/30 14:30:19 paul Exp $ |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 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 | # |
paul | dc830cb | 2005-04-16 15:51:05 +0000 | [diff] [blame] | 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 |
paul | d5c9253 | 2005-05-23 12:33:58 +0000 | [diff] [blame] | 12 | # characters [[:upper:]_[:digit:]]. Eg: |
paul | dc830cb | 2005-04-16 15:51:05 +0000 | [diff] [blame] | 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 | # |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 31 | |
| 32 | BEGIN { |
| 33 | mlistregex = "memory_list_(.*)\\[\\]"; |
paul | d5c9253 | 2005-05-23 12:33:58 +0000 | [diff] [blame] | 34 | mtyperegex = "^(MTYPE_[[:upper:]_[:digit:]]+).*"; |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 35 | header = "/* Auto-generated from memtypes.c by " ARGV[0] ". */\n"; |
| 36 | header = header "/* Do not edit! */\n"; |
| 37 | header = header "\n#ifndef _QUAGGA_MEMTYPES_H\n"; |
| 38 | header = header "#define _QUAGGA_MEMTYPES_H\n"; |
| 39 | footer = "\n#endif /* _QUAGGA_MEMTYPES_H */\n\n"; |
| 40 | mlistformat = "extern struct memory_list memory_list_%s[];"; |
| 41 | printf ("%s\n", header); |
| 42 | } |
| 43 | |
paul | dc830cb | 2005-04-16 15:51:05 +0000 | [diff] [blame] | 44 | # catch lines beginning with 'struct memory list ' and try snag the |
| 45 | # memory_list name. Has to be 3rd field. |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 46 | ($0 ~ /^struct memory_list /) && (NF >= 3) { |
Paul Jakma | d4ce4f6 | 2006-03-30 14:30:19 +0000 | [diff] [blame] | 47 | mlists[lcount++] = gensub(mlistregex, "\\1", "g",$3); |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
paul | dc830cb | 2005-04-16 15:51:05 +0000 | [diff] [blame] | 50 | # snag the MTYPE, it must self-standing and the second field, |
| 51 | # though we do manage to tolerate the , C seperator being appended |
| 52 | ($1 !~ /^\/?\*/) && ($2 ~ /^MTYPE_/) { |
Paul Jakma | d4ce4f6 | 2006-03-30 14:30:19 +0000 | [diff] [blame] | 53 | mtype[tcount++] = gensub(mtyperegex, "\\1", "g", $2); |
paul | 2fd2fd5 | 2005-04-15 11:47:15 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | END { |
| 57 | printf("enum\n{\n MTYPE_TMP = 1,\n"); |
| 58 | for (i = 0; i < tcount; i++) { |
| 59 | if (mtype[i] != "" && mtype[i] != "MTYPE_TMP") |
| 60 | printf (" %s,\n", mtype[i]); |
| 61 | } |
| 62 | printf (" MTYPE_MAX,\n};\n\n"); |
| 63 | for (i = 0; i < lcount; i++) { |
| 64 | if (mlists[i] != "") |
| 65 | printf (mlistformat "\n", mlists[i]); |
| 66 | } |
| 67 | printf (footer); |
| 68 | } |