From mboxrd@z Thu Jan 1 00:00:00 1970 From: Piotr Azarewicz Subject: [PATCH v2 1/1] cmdline: add any multi string mode to token string Date: Tue, 5 Apr 2016 10:47:58 +0200 Message-ID: <1459846078-2369-1-git-send-email-piotrx.t.azarewicz@intel.com> References: <1459510581-31392-1-git-send-email-piotrx.t.azarewicz@intel.com> Cc: dev@dpdk.org, Piotr Azarewicz To: olivier.matz@6wind.com Return-path: Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id 8BD7A28F2 for ; Tue, 5 Apr 2016 10:44:25 +0200 (CEST) In-Reply-To: <1459510581-31392-1-git-send-email-piotrx.t.azarewicz@intel.com> List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" While parsing token string there may be several modes: - fixed single string - multi-choice single string - any single string This patch add one more mode - any multi string. Signed-off-by: Piotr Azarewicz --- v2 changes: - add cmdline_multi_string_t type for the new mode app/test/test_cmdline_string.c | 15 ++++++---- lib/librte_cmdline/cmdline_parse.c | 8 ++++++ lib/librte_cmdline/cmdline_parse.h | 3 ++ lib/librte_cmdline/cmdline_parse_string.c | 43 +++++++++++++++++++++-------- lib/librte_cmdline/cmdline_parse_string.h | 7 +++++ 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/test/test_cmdline_string.c b/app/test/test_cmdline_string.c index 915a7d7..c5bb9c0 100644 --- a/app/test/test_cmdline_string.c +++ b/app/test/test_cmdline_string.c @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -65,9 +66,10 @@ struct string_elt_str string_elt_strs[] = { {"one#two\nwith\nnewlines#three", "two\nwith\nnewlines", 1}, }; -#if CMDLINE_TEST_BUFSIZE < STR_TOKEN_SIZE +#if (CMDLINE_TEST_BUFSIZE < STR_TOKEN_SIZE) \ +|| (CMDLINE_TEST_BUFSIZE < STR_MULTI_TOKEN_SIZE) #undef CMDLINE_TEST_BUFSIZE -#define CMDLINE_TEST_BUFSIZE STR_TOKEN_SIZE +#define CMDLINE_TEST_BUFSIZE RTE_MAX(STR_TOKEN_SIZE, STR_MULTI_TOKEN_SIZE) #endif struct string_nb_str { @@ -97,6 +99,11 @@ struct string_parse_str string_parse_strs[] = { {"two with\rgarbage\tcharacters\n", "one#two with\rgarbage\tcharacters\n#three", "two with\rgarbage\tcharacters\n"}, + {"one two", "one", "one"}, /* fixed string */ + {"one two", TOKEN_STRING_MULTI, "one two"}, /* multi string */ + {"one two", NULL, "one"}, /* any string */ + {"one two #three", TOKEN_STRING_MULTI, "one two "}, + /* multi string with comment */ }; @@ -124,7 +131,6 @@ struct string_invalid_str string_invalid_strs[] = { "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!" "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!" "toolong!!!" }, - {"invalid", ""}, {"", "invalid"} }; @@ -350,8 +356,7 @@ test_parse_string_valid(void) string_parse_strs[i].str, help_str); return -1; } - if (strncmp(buf, string_parse_strs[i].result, - sizeof(string_parse_strs[i].result) - 1) != 0) { + if (strcmp(buf, string_parse_strs[i].result) != 0) { printf("Error: result mismatch!\n"); return -1; } diff --git a/lib/librte_cmdline/cmdline_parse.c b/lib/librte_cmdline/cmdline_parse.c index 24a6ed6..b496067 100644 --- a/lib/librte_cmdline/cmdline_parse.c +++ b/lib/librte_cmdline/cmdline_parse.c @@ -118,6 +118,14 @@ cmdline_isendoftoken(char c) return 0; } +int +cmdline_isendofcommand(char c) +{ + if (!c || iscomment(c) || isendofline(c)) + return 1; + return 0; +} + static unsigned int nb_common_chars(const char * s1, const char * s2) { diff --git a/lib/librte_cmdline/cmdline_parse.h b/lib/librte_cmdline/cmdline_parse.h index 4b25c45..4ac05d6 100644 --- a/lib/librte_cmdline/cmdline_parse.h +++ b/lib/librte_cmdline/cmdline_parse.h @@ -184,6 +184,9 @@ int cmdline_complete(struct cmdline *cl, const char *buf, int *state, * isendofline(c)) */ int cmdline_isendoftoken(char c); +/* return true if(!c || iscomment(c) || isendofline(c)) */ +int cmdline_isendofcommand(char c); + #ifdef __cplusplus } #endif diff --git a/lib/librte_cmdline/cmdline_parse_string.c b/lib/librte_cmdline/cmdline_parse_string.c index 45883b3..35917a7 100644 --- a/lib/librte_cmdline/cmdline_parse_string.c +++ b/lib/librte_cmdline/cmdline_parse_string.c @@ -76,9 +76,10 @@ struct cmdline_token_ops cmdline_token_string_ops = { .get_help = cmdline_get_help_string, }; -#define MULTISTRING_HELP "Mul-choice STRING" -#define ANYSTRING_HELP "Any STRING" -#define FIXEDSTRING_HELP "Fixed STRING" +#define CHOICESTRING_HELP "Mul-choice STRING" +#define ANYSTRING_HELP "Any STRING" +#define ANYSTRINGS_HELP "Any STRINGS" +#define FIXEDSTRING_HELP "Fixed STRING" static unsigned int get_token_len(const char *s) @@ -123,8 +124,8 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, sd = &tk2->string_data; - /* fixed string */ - if (sd->str) { + /* fixed string (known single token) */ + if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) != 0)) { str = sd->str; do { token_len = get_token_len(str); @@ -148,7 +149,21 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, if (!str) return -1; } - /* unspecified string */ + /* multi string */ + else if (sd->str != NULL) { + if (ressize < STR_MULTI_TOKEN_SIZE) + return -1; + + token_len = 0; + while (!cmdline_isendofcommand(buf[token_len]) && + token_len < (STR_MULTI_TOKEN_SIZE - 1)) + token_len++; + + /* return if token too long */ + if (token_len >= (STR_MULTI_TOKEN_SIZE - 1)) + return -1; + } + /* unspecified string (unknown single token) */ else { token_len = 0; while(!cmdline_isendoftoken(buf[token_len]) && @@ -162,12 +177,16 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, } if (res) { - /* we are sure that token_len is < STR_TOKEN_SIZE-1 */ - snprintf(res, STR_TOKEN_SIZE, "%s", buf); + if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0)) + /* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */ + snprintf(res, STR_MULTI_TOKEN_SIZE, "%s", buf); + else + /* we are sure that token_len is < STR_TOKEN_SIZE-1 */ + snprintf(res, STR_TOKEN_SIZE, "%s", buf); + *((char *)res + token_len) = 0; } - return token_len; } @@ -242,8 +261,10 @@ int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf, s = sd->str; if (s) { - if (get_next_token(s)) - snprintf(dstbuf, size, MULTISTRING_HELP); + if (strcmp(s, TOKEN_STRING_MULTI) == 0) + snprintf(dstbuf, size, ANYSTRINGS_HELP); + else if (get_next_token(s)) + snprintf(dstbuf, size, CHOICESTRING_HELP); else snprintf(dstbuf, size, FIXEDSTRING_HELP); } else diff --git a/lib/librte_cmdline/cmdline_parse_string.h b/lib/librte_cmdline/cmdline_parse_string.h index 94aa1f1..3ceac57 100644 --- a/lib/librte_cmdline/cmdline_parse_string.h +++ b/lib/librte_cmdline/cmdline_parse_string.h @@ -70,8 +70,13 @@ extern "C" { /* size of a parsed string */ #define STR_TOKEN_SIZE 128 +/* size of a parsed multi string */ +#define STR_MULTI_TOKEN_SIZE 4096 + typedef char cmdline_fixed_string_t[STR_TOKEN_SIZE]; +typedef char cmdline_multi_string_t[STR_MULTI_TOKEN_SIZE]; + struct cmdline_token_string_data { const char *str; }; @@ -92,6 +97,8 @@ int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx, int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size); +#define TOKEN_STRING_MULTI "" + #define TOKEN_STRING_INITIALIZER(structure, field, string) \ { \ /* hdr */ \ -- 1.7.9.5