1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation.
* Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
* All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <eal_export.h>
#include <rte_string_fns.h>
#include "cmdline_parse.h"
#include "cmdline_parse_string.h"
RTE_EXPORT_SYMBOL(cmdline_token_string_ops)
struct cmdline_token_ops cmdline_token_string_ops = {
.parse = cmdline_parse_string,
.complete_get_nb = cmdline_complete_get_nb_string,
.complete_get_elt = cmdline_complete_get_elt_string,
.get_help = cmdline_get_help_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)
{
char c;
unsigned int i=0;
c = s[i];
while (c!='#' && c!='\0') {
i++;
c = s[i];
}
return i;
}
static const char *
get_next_token(const char *s)
{
unsigned int i;
i = get_token_len(s);
if (s[i] == '#')
return s+i+1;
return NULL;
}
RTE_EXPORT_SYMBOL(cmdline_parse_string)
int
cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
unsigned ressize)
{
struct cmdline_token_string *tk2;
struct cmdline_token_string_data *sd;
unsigned int token_len;
const char *str;
if (res && ressize < STR_TOKEN_SIZE)
return -1;
if (!tk || !buf || ! *buf)
return -1;
tk2 = (struct cmdline_token_string *)tk;
sd = &tk2->string_data;
/* 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 token is too big... */
if (token_len >= STR_TOKEN_SIZE - 1) {
continue;
}
if ( strncmp(buf, str, token_len) ) {
continue;
}
if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
continue;
}
break;
} while ( (str = get_next_token(str)) != NULL );
if (!str)
return -1;
}
/* 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]) &&
token_len < (STR_TOKEN_SIZE-1))
token_len++;
/* return if token too long */
if (token_len >= STR_TOKEN_SIZE - 1) {
return -1;
}
}
if (res) {
if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
/* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */
strlcpy(res, buf, STR_MULTI_TOKEN_SIZE);
else
/* we are sure that token_len is < STR_TOKEN_SIZE-1 */
strlcpy(res, buf, STR_TOKEN_SIZE);
*((char *)res + token_len) = 0;
}
return token_len;
}
RTE_EXPORT_SYMBOL(cmdline_complete_get_nb_string)
int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
{
struct cmdline_token_string *tk2;
struct cmdline_token_string_data *sd;
const char *str;
int ret = 1;
if (!tk)
return -1;
tk2 = (struct cmdline_token_string *)tk;
sd = &tk2->string_data;
if (!sd->str)
return 0;
str = sd->str;
while( (str = get_next_token(str)) != NULL ) {
ret++;
}
return ret;
}
RTE_EXPORT_SYMBOL(cmdline_complete_get_elt_string)
int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
char *dstbuf, unsigned int size)
{
struct cmdline_token_string *tk2;
struct cmdline_token_string_data *sd;
const char *s;
unsigned int len;
if (!tk || !dstbuf || idx < 0)
return -1;
tk2 = (struct cmdline_token_string *)tk;
sd = &tk2->string_data;
s = sd->str;
while (idx-- && s)
s = get_next_token(s);
if (!s)
return -1;
len = get_token_len(s);
if (len > size - 1)
return -1;
memcpy(dstbuf, s, len);
dstbuf[len] = '\0';
return 0;
}
RTE_EXPORT_SYMBOL(cmdline_get_help_string)
int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
unsigned int size)
{
struct cmdline_token_string *tk2;
struct cmdline_token_string_data *sd;
const char *s;
if (!tk || !dstbuf)
return -1;
tk2 = (struct cmdline_token_string *)tk;
sd = &tk2->string_data;
s = sd->str;
if (s) {
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
snprintf(dstbuf, size, ANYSTRING_HELP);
return 0;
}
|