1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
13 #include <rte_string_fns.h>
15 #include "cmdline_parse.h"
16 #include "cmdline_parse_string.h"
18 struct cmdline_token_ops cmdline_token_string_ops = {
19 .parse = cmdline_parse_string,
20 .complete_get_nb = cmdline_complete_get_nb_string,
21 .complete_get_elt = cmdline_complete_get_elt_string,
22 .get_help = cmdline_get_help_string,
25 #define CHOICESTRING_HELP "Mul-choice STRING"
26 #define ANYSTRING_HELP "Any STRING"
27 #define ANYSTRINGS_HELP "Any STRINGS"
28 #define FIXEDSTRING_HELP "Fixed STRING"
31 get_token_len(const char *s)
37 while (c!='#' && c!='\0') {
45 get_next_token(const char *s)
55 cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
58 struct cmdline_token_string *tk2;
59 struct cmdline_token_string_data *sd;
60 unsigned int token_len;
63 if (res && ressize < STR_TOKEN_SIZE)
66 if (!tk || !buf || ! *buf)
69 tk2 = (struct cmdline_token_string *)tk;
71 sd = &tk2->string_data;
73 /* fixed string (known single token) */
74 if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) != 0)) {
77 token_len = get_token_len(str);
79 /* if token is too big... */
80 if (token_len >= STR_TOKEN_SIZE - 1) {
84 if ( strncmp(buf, str, token_len) ) {
88 if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
93 } while ( (str = get_next_token(str)) != NULL );
99 else if (sd->str != NULL) {
100 if (ressize < STR_MULTI_TOKEN_SIZE)
104 while (!cmdline_isendofcommand(buf[token_len]) &&
105 token_len < (STR_MULTI_TOKEN_SIZE - 1))
108 /* return if token too long */
109 if (token_len >= (STR_MULTI_TOKEN_SIZE - 1))
112 /* unspecified string (unknown single token) */
115 while(!cmdline_isendoftoken(buf[token_len]) &&
116 token_len < (STR_TOKEN_SIZE-1))
119 /* return if token too long */
120 if (token_len >= STR_TOKEN_SIZE - 1) {
126 if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
127 /* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */
128 strlcpy(res, buf, STR_MULTI_TOKEN_SIZE);
130 /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
131 strlcpy(res, buf, STR_TOKEN_SIZE);
133 *((char *)res + token_len) = 0;
139 int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
141 struct cmdline_token_string *tk2;
142 struct cmdline_token_string_data *sd;
149 tk2 = (struct cmdline_token_string *)tk;
150 sd = &tk2->string_data;
156 while( (str = get_next_token(str)) != NULL ) {
162 int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
163 char *dstbuf, unsigned int size)
165 struct cmdline_token_string *tk2;
166 struct cmdline_token_string_data *sd;
170 if (!tk || !dstbuf || idx < 0)
173 tk2 = (struct cmdline_token_string *)tk;
174 sd = &tk2->string_data;
179 s = get_next_token(s);
184 len = get_token_len(s);
188 memcpy(dstbuf, s, len);
194 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
197 struct cmdline_token_string *tk2;
198 struct cmdline_token_string_data *sd;
204 tk2 = (struct cmdline_token_string *)tk;
205 sd = &tk2->string_data;
210 if (strcmp(s, TOKEN_STRING_MULTI) == 0)
211 snprintf(dstbuf, size, ANYSTRINGS_HELP);
212 else if (get_next_token(s))
213 snprintf(dstbuf, size, CHOICESTRING_HELP);
215 snprintf(dstbuf, size, FIXEDSTRING_HELP);
217 snprintf(dstbuf, size, ANYSTRING_HELP);