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