1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
9 #include <rte_string_fns.h>
11 #include "cmdline_parse.h"
12 #include "cmdline_parse_string.h"
14 struct cmdline_token_ops cmdline_token_string_ops = {
15 .parse = cmdline_parse_string,
16 .complete_get_nb = cmdline_complete_get_nb_string,
17 .complete_get_elt = cmdline_complete_get_elt_string,
18 .get_help = cmdline_get_help_string,
21 #define CHOICESTRING_HELP "Mul-choice STRING"
22 #define ANYSTRING_HELP "Any STRING"
23 #define ANYSTRINGS_HELP "Any STRINGS"
24 #define FIXEDSTRING_HELP "Fixed STRING"
27 get_token_len(const char *s)
33 while (c!='#' && c!='\0') {
41 get_next_token(const char *s)
51 cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
54 struct cmdline_token_string *tk2;
55 struct cmdline_token_string_data *sd;
56 unsigned int token_len;
59 if (res && ressize < STR_TOKEN_SIZE)
62 if (!tk || !buf || ! *buf)
65 tk2 = (struct cmdline_token_string *)tk;
67 sd = &tk2->string_data;
69 /* fixed string (known single token) */
70 if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) != 0)) {
73 token_len = get_token_len(str);
75 /* if token is too big... */
76 if (token_len >= STR_TOKEN_SIZE - 1) {
80 if ( strncmp(buf, str, token_len) ) {
84 if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
89 } while ( (str = get_next_token(str)) != NULL );
95 else if (sd->str != NULL) {
96 if (ressize < STR_MULTI_TOKEN_SIZE)
100 while (!cmdline_isendofcommand(buf[token_len]) &&
101 token_len < (STR_MULTI_TOKEN_SIZE - 1))
104 /* return if token too long */
105 if (token_len >= (STR_MULTI_TOKEN_SIZE - 1))
108 /* unspecified string (unknown single token) */
111 while(!cmdline_isendoftoken(buf[token_len]) &&
112 token_len < (STR_TOKEN_SIZE-1))
115 /* return if token too long */
116 if (token_len >= STR_TOKEN_SIZE - 1) {
122 if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
123 /* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */
124 strlcpy(res, buf, STR_MULTI_TOKEN_SIZE);
126 /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
127 strlcpy(res, buf, STR_TOKEN_SIZE);
129 *((char *)res + token_len) = 0;
135 int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
137 struct cmdline_token_string *tk2;
138 struct cmdline_token_string_data *sd;
145 tk2 = (struct cmdline_token_string *)tk;
146 sd = &tk2->string_data;
152 while( (str = get_next_token(str)) != NULL ) {
158 int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
159 char *dstbuf, unsigned int size)
161 struct cmdline_token_string *tk2;
162 struct cmdline_token_string_data *sd;
166 if (!tk || !dstbuf || idx < 0)
169 tk2 = (struct cmdline_token_string *)tk;
170 sd = &tk2->string_data;
175 s = get_next_token(s);
180 len = get_token_len(s);
184 memcpy(dstbuf, s, len);
190 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
193 struct cmdline_token_string *tk2;
194 struct cmdline_token_string_data *sd;
200 tk2 = (struct cmdline_token_string *)tk;
201 sd = &tk2->string_data;
206 if (strcmp(s, TOKEN_STRING_MULTI) == 0)
207 snprintf(dstbuf, size, ANYSTRINGS_HELP);
208 else if (get_next_token(s))
209 snprintf(dstbuf, size, CHOICESTRING_HELP);
211 snprintf(dstbuf, size, FIXEDSTRING_HELP);
213 snprintf(dstbuf, size, ANYSTRING_HELP);