blob: 59a7ab2dfc598ca2e496226ddd6f2b24fbbacfe8 [file] [log] [blame]
pauld5c92532005-05-23 12:33:58 +00001# $Id: memtypes.awk,v 1.3 2005/05/23 12:33:58 paul Exp $
paul2fd2fd52005-04-15 11:47:15 +00002#
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#
pauldc830cb2005-04-16 15:51:05 +00007# 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
pauld5c92532005-05-23 12:33:58 +000012# characters [[:upper:]_[:digit:]]. Eg:
pauldc830cb2005-04-16 15:51:05 +000013#
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#
paul2fd2fd52005-04-15 11:47:15 +000031
32BEGIN {
33 mlistregex = "memory_list_(.*)\\[\\]";
pauld5c92532005-05-23 12:33:58 +000034 mtyperegex = "^(MTYPE_[[:upper:]_[:digit:]]+).*";
paul2fd2fd52005-04-15 11:47:15 +000035 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
pauldc830cb2005-04-16 15:51:05 +000044# catch lines beginning with 'struct memory list ' and try snag the
45# memory_list name. Has to be 3rd field.
paul2fd2fd52005-04-15 11:47:15 +000046($0 ~ /^struct memory_list /) && (NF >= 3) {
47 mlists[lcount++] = gensub(mlistregex,"\\1",g,$3);
48}
49
pauldc830cb2005-04-16 15:51:05 +000050# 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_/) {
53 mtype[tcount++] = gensub(mtyperegex,"\\1",1, $2);
paul2fd2fd52005-04-15 11:47:15 +000054}
55
56END {
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}