diff options
author | Maria Matejka <mq@ucw.cz> | 2019-02-12 11:35:41 +0100 |
---|---|---|
committer | Maria Matejka <mq@ucw.cz> | 2019-02-20 22:30:54 +0100 |
commit | b256f241459c51224a4bf428f4bc5dfa44882920 (patch) | |
tree | 8c2f62f53911317e308a50deeedcbec5ae97c22d /filter | |
parent | 041608129ab15b5eab6fb607c45ddd2d748295b5 (diff) |
Filter: auto-generating enum-to-string
Diffstat (limited to 'filter')
-rw-r--r-- | filter/decl.m4 | 24 | ||||
-rw-r--r-- | filter/filter.c | 69 |
2 files changed, 24 insertions, 69 deletions
diff --git a/filter/decl.m4 b/filter/decl.m4 index 90ba4ff9..9c35677e 100644 --- a/filter/decl.m4 +++ b/filter/decl.m4 @@ -9,6 +9,7 @@ m4_divert(-1)m4_dnl # # Global Diversions: # 4 enum fi_code +# 5 enum fi_code to string # 1 struct f_inst_FI_... # 2 union in struct f_inst # 3 constructors @@ -30,6 +31,7 @@ m4_define(FID_STRUCT, `FID_ZONE(1, Per-instruction structure)') m4_define(FID_UNION, `FID_ZONE(2, Union member)') m4_define(FID_NEW, `FID_ZONE(3, Constructor)') m4_define(FID_ENUM, `FID_ZONE(4, Code enum)') +m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)') m4_define(FID_STRUCT_IN, `m4_divert(101)') m4_define(FID_NEW_ARGS, `m4_divert(102)') @@ -43,6 +45,8 @@ m4_define(FID_H, `m4_ifelse(TARGET, [[H]], FID_ALL, [[m4_define(FID_CURDIV, m4_d m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[ FID_ENUM INST_NAME(), +FID_ENUM_STR +[INST_NAME()] = "INST_NAME()", FID_STRUCT struct f_inst_[[]]INST_NAME() { m4_undivert(101) @@ -124,12 +128,32 @@ FID_C #include "nest/bird.h" #include "filter/filter.h" #include "filter/f-inst.h" + FID_H + /* Filter instruction codes */ enum f_instruction_code { FID_WR_PUT(4) }; +FID_C + +/* Instruction codes to string */ +static const char * const f_instruction_name_str[] = { +FID_WR_PUT(5) +}; + +const char * +f_instruction_name(enum f_instruction_code fi) +{ + if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0]))) + return f_instruction_name_str[fi]; + else + bug("Got unknown instruction code: %d", fi); +} + +FID_H + /* Per-instruction structures */ FID_WR_PUT(1) diff --git a/filter/filter.c b/filter/filter.c index 0bcf9836..ebd2e4dc 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -62,75 +62,6 @@ struct filter_state { void (*bt_assert_hook)(int result, const struct f_line_item *assert); -static const char * const f_instruction_name_str[] = { - /* TODO: Make this better */ - [FI_ADD] = "FI_ADD", - [FI_SUBTRACT] = "FI_SUBTRACT", - [FI_MULTIPLY] = "FI_MULTIPLY", - [FI_DIVIDE] = "FI_DIVIDE", - [FI_AND] = "FI_AND", - [FI_OR] = "FI_OR", - [FI_PAIR_CONSTRUCT] = "FI_PAIR_CONSTRUCT", - [FI_EC_CONSTRUCT] = "FI_EC_CONSTRUCT", - [FI_LC_CONSTRUCT] = "FI_LC_CONSTRUCT", - [FI_PATHMASK_CONSTRUCT] = "FI_PATHMASK_CONSTRUCT", - [FI_NEQ] = "FI_NEQ", - [FI_EQ] = "FI_EQ", - [FI_LT] = "FI_LT", - [FI_LTE] = "FI_LTE", - [FI_NOT] = "FI_NOT", - [FI_MATCH] = "FI_MATCH", - [FI_NOT_MATCH] = "FI_NOT_MATCH", - [FI_DEFINED] = "FI_DEFINED", - [FI_TYPE] = "FI_TYPE", - [FI_IS_V4] = "FI_IS_V4", - [FI_SET] = "FI_SET", - [FI_CONSTANT] = "FI_CONSTANT", - [FI_VARIABLE] = "FI_VARIABLE", - [FI_CONSTANT_INDIRECT] = "FI_CONSTANT_INDIRECT", - [FI_PRINT] = "FI_PRINT", - [FI_CONDITION] = "FI_CONDITION", - [FI_PRINT_AND_DIE] = "FI_PRINT_AND_DIE", - [FI_RTA_GET] = "FI_RTA_GET", - [FI_RTA_SET] = "FI_RTA_SET", - [FI_EA_GET] = "FI_EA_GET", - [FI_EA_SET] = "FI_EA_SET", - [FI_EA_UNSET] = "FI_EA_UNSET", - [FI_PREF_GET] = "FI_PREF_GET", - [FI_PREF_SET] = "FI_PREF_SET", - [FI_LENGTH] = "FI_LENGTH", - [FI_SADR_SRC] = "FI_SADR_SRC", - [FI_ROA_MAXLEN] = "FI_ROA_MAXLEN", - [FI_ROA_ASN] = "FI_ROA_ASN", - [FI_IP] = "FI_IP", - [FI_ROUTE_DISTINGUISHER] = "FI_ROUTE_DISTINGUISHER", - [FI_AS_PATH_FIRST] = "FI_AS_PATH_FIRST", - [FI_AS_PATH_LAST] = "FI_AS_PATH_LAST", - [FI_AS_PATH_LAST_NAG] = "FI_AS_PATH_LAST_NAG", - [FI_RETURN] = "FI_RETURN", - [FI_CALL] = "FI_CALL", - [FI_DROP_RESULT] = "FI_DROP_RESULT", - [FI_SWITCH] = "FI_SWITCH", - [FI_IP_MASK] = "FI_IP_MASK", - [FI_PATH_PREPEND] = "FI_PATH_PREPEND", - [FI_CLIST_ADD] = "FI_CLIST_ADD", - [FI_CLIST_DEL] = "FI_CLIST_DEL", - [FI_CLIST_FILTER] = "FI_CLIST_FILTER", - [FI_ROA_CHECK_IMPLICIT] = "FI_ROA_CHECK_IMPLICIT", - [FI_ROA_CHECK_EXPLICIT] = "FI_ROA_CHECK_EXPLICIT", - [FI_FORMAT] = "FI_FORMAT", - [FI_ASSERT] = "FI_ASSERT", -}; - -const char * -f_instruction_name(enum f_instruction_code fi) -{ - if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0]))) - return f_instruction_name_str[fi]; - else - bug("Got unknown instruction code: %d", fi); -} - static inline void f_cache_eattrs(struct filter_state *fs) { fs->eattrs = &((*fs->rte)->attrs->eattrs); |