diff options
author | Dave Barach <dave@barachs.net> | 2016-07-08 08:13:45 -0400 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2016-07-08 14:18:22 +0000 |
commit | 9b8ffd99fb35d37250cfb7094a0a31515f1bb577 (patch) | |
tree | 604ebcf86671a86b9e3395dda8d9fda50db2fcbc /vlib/example/plex_test.c | |
parent | 0557a91ca727cee963a8179808d2d2108564ec56 (diff) |
fd-io-styleify pass
Change-Id: If2d57a213fc2fec996db26df332910c3d2105f97
Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'vlib/example/plex_test.c')
-rw-r--r-- | vlib/example/plex_test.c | 371 |
1 files changed, 204 insertions, 167 deletions
diff --git a/vlib/example/plex_test.c b/vlib/example/plex_test.c index 947f25bc..ce0c8ef1 100644 --- a/vlib/example/plex_test.c +++ b/vlib/example/plex_test.c @@ -15,23 +15,22 @@ #include <vlib/parse.h> #include <vlib/unix/unix.h> -static u8 * format_value_v4_address (u8 * s, va_list * args) +static u8 * +format_value_v4_address (u8 * s, va_list * args) { vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *); u32 a = v->value.as_uword; s = format (s, "%d.%d.%d.%d", - (a>>24) & 0xFF, - (a>>16) & 0xFF, - (a>>8) & 0xFF, - (a>>0) & 0xFF); + (a >> 24) & 0xFF, + (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a >> 0) & 0xFF); return s; } static vlib_parse_match_t -v4_address_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, - vlib_lex_token_t *t, vlib_parse_value_t *valuep) +v4_address_match (vlib_parse_main_t * pm, vlib_parse_type_t * type, + vlib_lex_token_t * t, vlib_parse_value_t * valuep) { u32 digit; u32 value = 0; @@ -42,19 +41,23 @@ v4_address_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */ - for (i = 0; i < 7; i++) { - if ((i & 1) == 0) { - if (t[i].token != VLIB_LEX_number) - return VLIB_PARSE_MATCH_FAIL; - if (t[i].value.as_uword > 0xff) - return VLIB_PARSE_MATCH_FAIL; - digit = t[i].value.as_uword; - value = (value << 8) | digit; - } else { - if (t[i].token != VLIB_LEX_dot) - return VLIB_PARSE_MATCH_FAIL; + for (i = 0; i < 7; i++) + { + if ((i & 1) == 0) + { + if (t[i].token != VLIB_LEX_number) + return VLIB_PARSE_MATCH_FAIL; + if (t[i].value.as_uword > 0xff) + return VLIB_PARSE_MATCH_FAIL; + digit = t[i].value.as_uword; + value = (value << 8) | digit; + } + else + { + if (t[i].token != VLIB_LEX_dot) + return VLIB_PARSE_MATCH_FAIL; + } } - } /* note: caller advances by 1 */ pm->current_token_index += 6; valuep->value.as_uword = value; @@ -62,25 +65,22 @@ v4_address_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, } PARSE_TYPE_INIT (v4_address, v4_address_match, 0, format_value_v4_address) - -static u8 * format_value_v4_address_and_mask (u8 * s, va_list * args) + static u8 *format_value_v4_address_and_mask (u8 * s, va_list * args) { vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *); - u32 * a = v->value.as_pointer; + u32 *a = v->value.as_pointer; s = format (s, "%d.%d.%d.%d", - (a[0]>>24) & 0xFF, - (a[0]>>16) & 0xFF, - (a[0]>>8) & 0xFF, - (a[0]>>0) & 0xFF); + (a[0] >> 24) & 0xFF, + (a[0] >> 16) & 0xFF, (a[0] >> 8) & 0xFF, (a[0] >> 0) & 0xFF); s = format (s, "/%d", a[1]); return s; } static vlib_parse_match_t -v4_address_and_mask_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, - vlib_lex_token_t *t, vlib_parse_value_t *valuep) +v4_address_and_mask_match (vlib_parse_main_t * pm, vlib_parse_type_t * type, + vlib_lex_token_t * t, vlib_parse_value_t * valuep) { u32 digit; u32 address = 0; @@ -92,19 +92,23 @@ v4_address_and_mask_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */ - for (i = 0; i < 7; i++) { - if ((i & 1) == 0) { - if (t[i].token != VLIB_LEX_number) - return VLIB_PARSE_MATCH_FAIL; - if (t[i].value.as_uword > 0xff) - return VLIB_PARSE_MATCH_FAIL; - digit = t[i].value.as_uword; - address = (address << 8) | digit; - } else { - if (t[i].token != VLIB_LEX_dot) - return VLIB_PARSE_MATCH_FAIL; + for (i = 0; i < 7; i++) + { + if ((i & 1) == 0) + { + if (t[i].token != VLIB_LEX_number) + return VLIB_PARSE_MATCH_FAIL; + if (t[i].value.as_uword > 0xff) + return VLIB_PARSE_MATCH_FAIL; + digit = t[i].value.as_uword; + address = (address << 8) | digit; + } + else + { + if (t[i].token != VLIB_LEX_dot) + return VLIB_PARSE_MATCH_FAIL; + } } - } if (t[7].token != VLIB_LEX_slash || t[8].token != VLIB_LEX_number) return VLIB_PARSE_MATCH_FAIL; @@ -118,135 +122,143 @@ v4_address_and_mask_match (vlib_parse_main_t *pm, vlib_parse_type_t *type, return VLIB_PARSE_MATCH_VALUE; } -void v4_address_and_mask_cleanup (vlib_parse_value_t *valuep) +void +v4_address_and_mask_cleanup (vlib_parse_value_t * valuep) { - u32 * trash = valuep->value.as_pointer; + u32 *trash = valuep->value.as_pointer; vec_free (trash); } -PARSE_TYPE_INIT (v4_address_and_mask, v4_address_and_mask_match, - v4_address_and_mask_cleanup, - format_value_v4_address_and_mask) - -vlib_lex_main_t vlib_lex_main; +PARSE_TYPE_INIT (v4_address_and_mask, v4_address_and_mask_match, + v4_address_and_mask_cleanup, + format_value_v4_address_and_mask) + vlib_lex_main_t vlib_lex_main; -vlib_parse_match_t eval_factor0 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + vlib_parse_match_t eval_factor0 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, + vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_factor1 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_factor1 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_factor2 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_factor2 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { word a; - int index = vec_len (pm->parse_value)-1; - - a = pm->parse_value [index].value.as_word; - + int index = vec_len (pm->parse_value) - 1; + + a = pm->parse_value[index].value.as_word; + pm->parse_value[index].value.as_word = -a; return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_term0 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_term0 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_term1 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_term1 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; - int index = vec_len (pm->parse_value)-2; - - a = pm->parse_value [index].value.as_uword; - b = pm->parse_value [index+1].value.as_uword; - + int index = vec_len (pm->parse_value) - 2; + + a = pm->parse_value[index].value.as_uword; + b = pm->parse_value[index + 1].value.as_uword; + pm->parse_value[index].value.as_uword = a * b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); - + return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_term2 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_term2 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; - int index = vec_len (pm->parse_value)-2; - - a = pm->parse_value [index].value.as_uword; - b = pm->parse_value [index+1].value.as_uword; - + int index = vec_len (pm->parse_value) - 2; + + a = pm->parse_value[index].value.as_uword; + b = pm->parse_value[index + 1].value.as_uword; + pm->parse_value[index].value.as_uword = a / b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); - + return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_exp0 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_exp0 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_exp1 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_exp1 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; - int index = vec_len (pm->parse_value)-2; - - a = pm->parse_value [index].value.as_uword; - b = pm->parse_value [index+1].value.as_uword; - + int index = vec_len (pm->parse_value) - 2; + + a = pm->parse_value[index].value.as_uword; + b = pm->parse_value[index + 1].value.as_uword; + pm->parse_value[index].value.as_uword = a + b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); - + return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_exp2 (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) + +vlib_parse_match_t +eval_exp2 (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; - int index = vec_len (pm->parse_value)-2; - - a = pm->parse_value [index].value.as_uword; - b = pm->parse_value [index+1].value.as_uword; - + int index = vec_len (pm->parse_value) - 2; + + a = pm->parse_value[index].value.as_uword; + b = pm->parse_value[index + 1].value.as_uword; + pm->parse_value[index].value.as_uword = a - b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); - + return VLIB_PARSE_MATCH_RULE; } -vlib_parse_match_t eval_result (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) +vlib_parse_match_t +eval_result (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_DONE; } -vlib_parse_match_t noop_match_rule (vlib_parse_main_t *pm, - vlib_parse_item_t *item, - vlib_parse_value_t *value) +vlib_parse_match_t +noop_match_rule (vlib_parse_main_t * pm, + vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; @@ -330,18 +342,17 @@ vlib_parse_match_t eval##n (vlib_parse_main_t *pm, \ return VLIB_PARSE_MATCH_DONE; \ } foreach_rule_evaluator - #undef _ - PARSE_INIT (r1, "eval <moo>", eval_result); PARSE_INIT (r2, "<moo> = cow", eval0); PARSE_INIT (r4, "<moo> = ", eval1); PARSE_TYPE_INIT (moo, rule_match, 0, 0); -#endif +#endif -clib_error_t *test_init (vlib_main_t *vm) +clib_error_t * +test_init (vlib_main_t * vm) { clib_error_t *error; @@ -356,13 +367,13 @@ VLIB_INIT_FUNCTION (test_init); clib_error_t * vlib_stdlex_init (vlib_main_t * vm) { - vlib_lex_main_t * lm = &vlib_lex_main; + vlib_lex_main_t *lm = &vlib_lex_main; u16 top_index; u16 slash_index, slash_star_index, slash_slash_index, slash_star_star_index; u16 slash_token; u16 word_index; u16 zero_index, octal_index, decimal_index, hex_index, binary_index; - + top_index = vlib_lex_add_table ("top"); #define foreach_top_level_single_character_token \ @@ -395,43 +406,52 @@ vlib_stdlex_init (vlib_main_t * vm) binary_index = vlib_lex_add_table ("binary"); /* Support 0x 0b 0t and 0123 [octal] */ - vlib_lex_set_action_range (top_index, '0', '0', VLIB_LEX_START_NUMBER, 10, zero_index); - vlib_lex_set_action_range (top_index, '1', '9', VLIB_LEX_START_NUMBER, 10, decimal_index); - - vlib_lex_set_action_range (zero_index, 0, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); + vlib_lex_set_action_range (top_index, '0', '0', VLIB_LEX_START_NUMBER, 10, + zero_index); + vlib_lex_set_action_range (top_index, '1', '9', VLIB_LEX_START_NUMBER, 10, + decimal_index); + + vlib_lex_set_action_range (zero_index, 0, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, + VLIB_LEX_number, top_index); + + vlib_lex_set_action_range (zero_index, 'x', 'x', VLIB_LEX_IGNORE, ~0, + hex_index); + vlib_lex_set_action_range (zero_index, 'b', 'b', VLIB_LEX_IGNORE, ~0, + binary_index); + vlib_lex_set_action_range (zero_index, 't', 't', VLIB_LEX_IGNORE, ~0, + decimal_index); + vlib_lex_set_action_range (zero_index, '0', '7', VLIB_LEX_START_NUMBER, 8, + octal_index); - vlib_lex_set_action_range (zero_index, 'x', 'x', VLIB_LEX_IGNORE, ~0, hex_index); - vlib_lex_set_action_range (zero_index, 'b', 'b', VLIB_LEX_IGNORE, ~0, binary_index); - vlib_lex_set_action_range (zero_index, 't', 't', VLIB_LEX_IGNORE, ~0, decimal_index); - vlib_lex_set_action_range (zero_index, '0', '7', VLIB_LEX_START_NUMBER, 8, octal_index); - /* Octal */ vlib_lex_set_action_range (octal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, - VLIB_LEX_number, top_index); - vlib_lex_set_action_range (octal_index, '0', '7', VLIB_LEX_ADD_TO_NUMBER, 8, - octal_index); + VLIB_LEX_number, top_index); + vlib_lex_set_action_range (octal_index, '0', '7', VLIB_LEX_ADD_TO_NUMBER, 8, + octal_index); /* Decimal */ - vlib_lex_set_action_range (decimal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, - VLIB_LEX_number, top_index); - vlib_lex_set_action_range (decimal_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 10, - decimal_index); - + vlib_lex_set_action_range (decimal_index, 0, 0x7f, + VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, + top_index); + vlib_lex_set_action_range (decimal_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, + 10, decimal_index); + /* Hex */ vlib_lex_set_action_range (hex_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, - VLIB_LEX_number, top_index); - vlib_lex_set_action_range (hex_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 16, - hex_index); - vlib_lex_set_action_range (hex_index, 'a', 'f', VLIB_LEX_ADD_TO_NUMBER, 16, - hex_index); - vlib_lex_set_action_range (hex_index, 'A', 'F', VLIB_LEX_ADD_TO_NUMBER, 16, - hex_index); - + VLIB_LEX_number, top_index); + vlib_lex_set_action_range (hex_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 16, + hex_index); + vlib_lex_set_action_range (hex_index, 'a', 'f', VLIB_LEX_ADD_TO_NUMBER, 16, + hex_index); + vlib_lex_set_action_range (hex_index, 'A', 'F', VLIB_LEX_ADD_TO_NUMBER, 16, + hex_index); + /* Binary */ - vlib_lex_set_action_range (binary_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, - VLIB_LEX_number, top_index); - vlib_lex_set_action_range (binary_index, '0', '1', VLIB_LEX_ADD_TO_NUMBER, 2, - binary_index); + vlib_lex_set_action_range (binary_index, 0, 0x7f, + VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, + top_index); + vlib_lex_set_action_range (binary_index, '0', '1', VLIB_LEX_ADD_TO_NUMBER, + 2, binary_index); /* c/c++ comment syntax is the worst... */ @@ -442,49 +462,66 @@ vlib_stdlex_init (vlib_main_t * vm) slash_token = vlib_lex_add_token (lm, "slash"); /* Top level: see a slash, ignore, go to slash table */ - vlib_lex_set_action_range (top_index, '/', '/', VLIB_LEX_IGNORE, ~0, slash_index); + vlib_lex_set_action_range (top_index, '/', '/', VLIB_LEX_IGNORE, ~0, + slash_index); /* default for slash table: return SLASH, go to top table */ - vlib_lex_set_action_range (slash_index, 1, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, slash_token, - top_index); + vlib_lex_set_action_range (slash_index, 1, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, + slash_token, top_index); /* see slash-slash, go to s-s table */ - vlib_lex_set_action_range (slash_index, '/', '/', VLIB_LEX_IGNORE, ~0, - slash_slash_index); + vlib_lex_set_action_range (slash_index, '/', '/', VLIB_LEX_IGNORE, ~0, + slash_slash_index); /* see slash-star, go to s-* table */ - vlib_lex_set_action_range (slash_index, '*', '*', VLIB_LEX_IGNORE, ~0, - slash_star_index); + vlib_lex_set_action_range (slash_index, '*', '*', VLIB_LEX_IGNORE, ~0, + slash_star_index); /* EOL in s-s table, ignore, go to top table */ - vlib_lex_set_action_range (slash_slash_index, '\n', '\n', VLIB_LEX_IGNORE, ~0, - top_index); + vlib_lex_set_action_range (slash_slash_index, '\n', '\n', VLIB_LEX_IGNORE, + ~0, top_index); /* slash-star blah blah star */ vlib_lex_set_action_range (slash_star_index, '*', '*', VLIB_LEX_IGNORE, ~0, - slash_star_star_index); + slash_star_star_index); /* slash star blah blah star slash */ - vlib_lex_set_action_range (slash_star_star_index, '/', '/', VLIB_LEX_IGNORE, ~0, - top_index); + vlib_lex_set_action_range (slash_star_star_index, '/', '/', VLIB_LEX_IGNORE, + ~0, top_index); /* LT, =, GT */ - vlib_lex_set_action_range (top_index, '<', '<', VLIB_LEX_RETURN, VLIB_LEX_lt, top_index); - vlib_lex_set_action_range (top_index, '=', '=', VLIB_LEX_RETURN, VLIB_LEX_equals, - top_index); - vlib_lex_set_action_range (top_index, '>', '>', VLIB_LEX_RETURN, VLIB_LEX_gt, top_index); + vlib_lex_set_action_range (top_index, '<', '<', VLIB_LEX_RETURN, + VLIB_LEX_lt, top_index); + vlib_lex_set_action_range (top_index, '=', '=', VLIB_LEX_RETURN, + VLIB_LEX_equals, top_index); + vlib_lex_set_action_range (top_index, '>', '>', VLIB_LEX_RETURN, + VLIB_LEX_gt, top_index); /* words, key and otherwise */ word_index = vlib_lex_add_table ("word"); - vlib_lex_set_action_range (top_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); - vlib_lex_set_action_range (top_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); + vlib_lex_set_action_range (top_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); + vlib_lex_set_action_range (top_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); - vlib_lex_set_action_range (word_index, 0, 0x7f, VLIB_LEX_KEYWORD_CHECK, ~0, top_index); + vlib_lex_set_action_range (word_index, 0, 0x7f, VLIB_LEX_KEYWORD_CHECK, ~0, + top_index); + + vlib_lex_set_action_range (word_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); + vlib_lex_set_action_range (word_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); + vlib_lex_set_action_range (word_index, '_', '_', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); + vlib_lex_set_action_range (word_index, '0', '9', VLIB_LEX_ADD_TO_TOKEN, ~0, + word_index); - vlib_lex_set_action_range (word_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); - vlib_lex_set_action_range (word_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); - vlib_lex_set_action_range (word_index, '_', '_', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); - vlib_lex_set_action_range (word_index, '0', '9', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); - return 0; } +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |