From 024e1957cc4c39bb30144e2c023151e35df473e1 Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Fri, 2 Oct 2020 18:23:38 +0700 Subject: vty: check for duplicate flags in application specific attributes This would facilitate detecting duplicates on early development stages. Change-Id: I4e27d6e89d3f851b5ea4f00da01e7093afa537b2 Related: SYS#4937 --- src/vty/vty.c | 16 ++++++++++++++++ tests/vty/vty_test.c | 18 ++++++++++++++++++ tests/vty/vty_test.err | 2 ++ 3 files changed, 36 insertions(+) diff --git a/src/vty/vty.c b/src/vty/vty.c index 6e7bdcb3..4d511655 100644 --- a/src/vty/vty.c +++ b/src/vty/vty.c @@ -66,6 +66,7 @@ #include #include #include +#include #ifndef MAXPATHLEN #define MAXPATHLEN 4096 @@ -1794,6 +1795,8 @@ void vty_init_vtysh(void) /* Install vty's own commands like `who' command. */ void vty_init(struct vty_app_info *app_info) { + unsigned int i, j; + tall_vty_ctx = talloc_named_const(NULL, 0, "vty"); tall_vty_vec_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_vector"); tall_vty_cmd_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_command"); @@ -1802,6 +1805,19 @@ void vty_init(struct vty_app_info *app_info) host.app_info = app_info; + /* Check for duplicate flags in application specific attributes (if any) */ + for (i = 0; i < ARRAY_SIZE(app_info->usr_attr_letters); i++) { + if (app_info->usr_attr_letters[i] == '\0') + continue; + for (j = i + 1; j < ARRAY_SIZE(app_info->usr_attr_letters); j++) { + if (app_info->usr_attr_letters[j] != app_info->usr_attr_letters[i]) + continue; + fprintf(stderr, "Found duplicate flag letter '%c' in application " + "specific attributes (index %u vs %u)! Please fix.\n", + app_info->usr_attr_letters[i], i, j); + } + } + /* For further configuration read, preserve current directory. */ vty_save_cwd(); diff --git a/tests/vty/vty_test.c b/tests/vty/vty_test.c index b2d34ad8..1608b830 100644 --- a/tests/vty/vty_test.c +++ b/tests/vty/vty_test.c @@ -513,11 +513,29 @@ void test_numeric_range() destroy_test_vty(&test, vty); } +/* Application specific attributes */ +enum vty_test_attr { + VTY_TEST_ATTR_FOO = 0, + VTY_TEST_ATTR_BAR, + VTY_TEST_ATTR_ZOO, + VTY_TEST_ATTR_FOO_DUP, + VTY_TEST_ATTR_ZOO_DUP, +}; + int main(int argc, char **argv) { struct vty_app_info vty_info = { .name = "VtyTest", .version = 0, + .usr_attr_letters = { + [VTY_TEST_ATTR_FOO] = 'f', + [VTY_TEST_ATTR_BAR] = 'b', + [VTY_TEST_ATTR_ZOO] = 'z', + + /* Duplicate detection check */ + [VTY_TEST_ATTR_FOO_DUP] = 'f', + [VTY_TEST_ATTR_ZOO_DUP] = 'z', + }, }; const struct log_info_cat default_categories[] = {}; diff --git a/tests/vty/vty_test.err b/tests/vty/vty_test.err index 8ad4fd97..25c94e62 100644 --- a/tests/vty/vty_test.err +++ b/tests/vty/vty_test.err @@ -1,3 +1,5 @@ +Found duplicate flag letter 'f' in application specific attributes (index 0 vs 3)! Please fix. +Found duplicate flag letter 'z' in application specific attributes (index 2 vs 4)! Please fix. Got VTY event: 2 Got VTY event: 2 Got VTY event: 2 -- cgit v1.2.3