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
51BEGIN {
52	mlistregex = "memory_list_(.*)\\[\\]";
53	mtyperegex = "^(MTYPE_[[:upper:]_[:digit:]]+).*";
54	header = "/* Auto-generated from memtypes.c by " ARGV[0] ". */\n";
55	header = header "/* Do not edit! */\n";
56	header = header "\n#ifndef _QUAGGA_MEMTYPES_H\n";
57	header = header "#define _QUAGGA_MEMTYPES_H\n";
58	footer = "\n#endif /* _QUAGGA_MEMTYPES_H */\n\n";
59	mlistformat = "extern struct memory_list memory_list_%s[];";
60	printf ("%s\n", header);
61}
62
63# catch lines beginning with 'struct memory list ' and try snag the
64# memory_list name. Has to be 3rd field.
65($0 ~ /^struct memory_list /) && (NF >= 3) {
66	mlists[lcount++] = gensub(mlistregex, "\\1", "g",$3);
67}
68
69# snag the MTYPE, it must self-standing and the second field,
70# though we do manage to tolerate the , C seperator being appended
71($1 !~ /^\/?\*/) && ($2 ~ /^MTYPE_/) { 
72	mtype[tcount++] = gensub(mtyperegex, "\\1", "g", $2);
73} 
74
75END {
76	printf("enum\n{\n  MTYPE_TMP = 1,\n"); 
77	for (i = 0; i < tcount; i++) {
78		if (mtype[i] != "" && mtype[i] != "MTYPE_TMP")
79			printf ("  %s,\n", mtype[i]);
80	}
81	printf ("  MTYPE_MAX,\n};\n\n");
82	for (i = 0; i < lcount; i++) {
83		if (mlists[i] != "")
84			printf (mlistformat "\n", mlists[i]);
85	}
86	printf (footer);
87}
88