#include <string.h>
#include <inttypes.h>
+#include <rte_common.h>
#include <rte_string_fns.h>
#include <cmdline_parse.h>
{"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 {
{"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 */
};
"toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
"toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
"toolong!!!" },
- {"invalid", ""},
{"", "invalid"}
};
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;
}
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)
{
* isendofline(c)) */
int cmdline_isendoftoken(char c);
+/* return true if(!c || iscomment(c) || isendofline(c)) */
+int cmdline_isendofcommand(char c);
+
#ifdef __cplusplus
}
#endif
.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)
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);
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]) &&
}
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;
}
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
/* 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;
};
int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
unsigned int size);
+/**
+* Token marked as TOKEN_STRING_MULTI takes entire parsing string
+* until “#” sign appear. Everything after “#” sign is treated as a comment.
+*
+* Note:
+* In this case second parameter of TOKEN_STRING_INITIALIZER must be a type of
+* cmdline_multi_string_t.
+*/
+#define TOKEN_STRING_MULTI ""
+
#define TOKEN_STRING_INITIALIZER(structure, field, string) \
{ \
/* hdr */ \