Commit | Line | Data |
---|---|---|
d7e09d03 PT |
1 | /* |
2 | * GPL HEADER START | |
3 | * | |
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License version 2 for more details (a copy is included | |
14 | * in the LICENSE file that accompanied this code). | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * version 2 along with this program; If not, see | |
6a5b99a4 | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
d7e09d03 | 19 | * |
d7e09d03 PT |
20 | * GPL HEADER END |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. | |
24 | * Use is subject to license terms. | |
25 | * | |
1dc563a6 | 26 | * Copyright (c) 2012, 2015 Intel Corporation. |
d7e09d03 PT |
27 | */ |
28 | /* | |
29 | * This file is part of Lustre, http://www.lustre.org/ | |
30 | * Lustre is a trademark of Sun Microsystems, Inc. | |
31 | * | |
32 | * String manipulation functions. | |
33 | * | |
34 | * libcfs/libcfs/libcfs_string.c | |
35 | * | |
36 | * Author: Nathan Rutman <nathan.rutman@sun.com> | |
37 | */ | |
38 | ||
9fdaf8c0 | 39 | #include "../../include/linux/libcfs/libcfs.h" |
d7e09d03 | 40 | |
d7e09d03 PT |
41 | /* Convert a text string to a bitmask */ |
42 | int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), | |
43 | int *oldmask, int minmask, int allmask) | |
44 | { | |
45 | const char *debugstr; | |
3f9773a5 | 46 | char op = '\0'; |
d7e09d03 | 47 | int newmask = minmask, i, len, found = 0; |
d7e09d03 PT |
48 | |
49 | /* <str> must be a list of tokens separated by whitespace | |
50 | * and optionally an operator ('+' or '-'). If an operator | |
51 | * appears first in <str>, '*oldmask' is used as the starting point | |
52 | * (relative), otherwise minmask is used (absolute). An operator | |
a3fbcb3c OD |
53 | * applies to all following tokens up to the next operator. |
54 | */ | |
3f9773a5 | 55 | while (*str != '\0') { |
d7e09d03 PT |
56 | while (isspace(*str)) |
57 | str++; | |
3f9773a5 | 58 | if (*str == '\0') |
d7e09d03 PT |
59 | break; |
60 | if (*str == '+' || *str == '-') { | |
61 | op = *str++; | |
62 | if (!found) | |
63 | /* only if first token is relative */ | |
64 | newmask = *oldmask; | |
65 | while (isspace(*str)) | |
66 | str++; | |
3f9773a5 | 67 | if (*str == '\0') /* trailing op */ |
d7e09d03 PT |
68 | return -EINVAL; |
69 | } | |
70 | ||
71 | /* find token length */ | |
3f9773a5 MT |
72 | len = 0; |
73 | while (str[len] != '\0' && !isspace(str[len]) && | |
74 | str[len] != '+' && str[len] != '-') | |
75 | len++; | |
d7e09d03 PT |
76 | |
77 | /* match token */ | |
78 | found = 0; | |
79 | for (i = 0; i < 32; i++) { | |
80 | debugstr = bit2str(i); | |
15d9f520 | 81 | if (debugstr && strlen(debugstr) == len && |
7a724582 | 82 | strncasecmp(str, debugstr, len) == 0) { |
d7e09d03 PT |
83 | if (op == '-') |
84 | newmask &= ~(1 << i); | |
85 | else | |
86 | newmask |= (1 << i); | |
87 | found = 1; | |
88 | break; | |
89 | } | |
90 | } | |
91 | if (!found && len == 3 && | |
7a724582 | 92 | (strncasecmp(str, "ALL", len) == 0)) { |
d7e09d03 PT |
93 | if (op == '-') |
94 | newmask = minmask; | |
95 | else | |
96 | newmask = allmask; | |
97 | found = 1; | |
98 | } | |
99 | if (!found) { | |
100 | CWARN("unknown mask '%.*s'.\n" | |
101 | "mask usage: [+|-]<all|type> ...\n", len, str); | |
102 | return -EINVAL; | |
103 | } | |
104 | str += len; | |
105 | } | |
106 | ||
107 | *oldmask = newmask; | |
108 | return 0; | |
109 | } | |
d7e09d03 | 110 | |
d7e09d03 PT |
111 | /* get the first string out of @str */ |
112 | char *cfs_firststr(char *str, size_t size) | |
113 | { | |
114 | size_t i = 0; | |
115 | char *end; | |
116 | ||
117 | /* trim leading spaces */ | |
118 | while (i < size && *str && isspace(*str)) { | |
119 | ++i; | |
120 | ++str; | |
121 | } | |
122 | ||
123 | /* string with all spaces */ | |
124 | if (*str == '\0') | |
125 | goto out; | |
126 | ||
127 | end = str; | |
128 | while (i < size && *end != '\0' && !isspace(*end)) { | |
129 | ++i; | |
130 | ++end; | |
131 | } | |
132 | ||
51566472 | 133 | *end = '\0'; |
d7e09d03 PT |
134 | out: |
135 | return str; | |
136 | } | |
137 | EXPORT_SYMBOL(cfs_firststr); | |
138 | ||
139 | char * | |
140 | cfs_trimwhite(char *str) | |
141 | { | |
142 | char *end; | |
143 | ||
e525a681 | 144 | while (isspace(*str)) |
d7e09d03 PT |
145 | str++; |
146 | ||
147 | end = str + strlen(str); | |
148 | while (end > str) { | |
e525a681 | 149 | if (!isspace(end[-1])) |
d7e09d03 PT |
150 | break; |
151 | end--; | |
152 | } | |
153 | ||
154 | *end = 0; | |
155 | return str; | |
156 | } | |
157 | EXPORT_SYMBOL(cfs_trimwhite); | |
158 | ||
159 | /** | |
160 | * Extracts tokens from strings. | |
161 | * | |
162 | * Looks for \a delim in string \a next, sets \a res to point to | |
163 | * substring before the delimiter, sets \a next right after the found | |
164 | * delimiter. | |
165 | * | |
166 | * \retval 1 if \a res points to a string of non-whitespace characters | |
167 | * \retval 0 otherwise | |
168 | */ | |
169 | int | |
170 | cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res) | |
171 | { | |
172 | char *end; | |
173 | ||
15d9f520 | 174 | if (!next->ls_str) |
d7e09d03 PT |
175 | return 0; |
176 | ||
177 | /* skip leading white spaces */ | |
178 | while (next->ls_len) { | |
e525a681 | 179 | if (!isspace(*next->ls_str)) |
d7e09d03 PT |
180 | break; |
181 | next->ls_str++; | |
182 | next->ls_len--; | |
183 | } | |
184 | ||
185 | if (next->ls_len == 0) /* whitespaces only */ | |
186 | return 0; | |
187 | ||
188 | if (*next->ls_str == delim) { | |
189 | /* first non-writespace is the delimiter */ | |
190 | return 0; | |
191 | } | |
192 | ||
193 | res->ls_str = next->ls_str; | |
194 | end = memchr(next->ls_str, delim, next->ls_len); | |
15d9f520 | 195 | if (!end) { |
d7e09d03 PT |
196 | /* there is no the delimeter in the string */ |
197 | end = next->ls_str + next->ls_len; | |
198 | next->ls_str = NULL; | |
199 | } else { | |
200 | next->ls_str = end + 1; | |
201 | next->ls_len -= (end - res->ls_str + 1); | |
202 | } | |
203 | ||
204 | /* skip ending whitespaces */ | |
205 | while (--end != res->ls_str) { | |
e525a681 | 206 | if (!isspace(*end)) |
d7e09d03 PT |
207 | break; |
208 | } | |
209 | ||
210 | res->ls_len = end - res->ls_str + 1; | |
211 | return 1; | |
212 | } | |
47ca6ec2 | 213 | EXPORT_SYMBOL(cfs_gettok); |
d7e09d03 PT |
214 | |
215 | /** | |
216 | * Converts string to integer. | |
217 | * | |
218 | * Accepts decimal and hexadecimal number recordings. | |
219 | * | |
220 | * \retval 1 if first \a nob chars of \a str convert to decimal or | |
221 | * hexadecimal integer in the range [\a min, \a max] | |
222 | * \retval 0 otherwise | |
223 | */ | |
224 | int | |
225 | cfs_str2num_check(char *str, int nob, unsigned *num, | |
226 | unsigned min, unsigned max) | |
227 | { | |
3ad6152d JS |
228 | bool all_numbers = true; |
229 | char *endp, cache; | |
ae0246da | 230 | int rc; |
d7e09d03 | 231 | |
3ad6152d JS |
232 | /** |
233 | * kstrouint can only handle strings composed | |
234 | * of only numbers. We need to scan the string | |
235 | * passed in for the first non-digit character | |
236 | * and end the string at that location. If we | |
237 | * don't find any non-digit character we still | |
238 | * need to place a '\0' at position nob since | |
239 | * we are not interested in the rest of the | |
240 | * string which is longer than nob in size. | |
241 | * After we are done the character at the | |
242 | * position we placed '\0' must be restored. | |
243 | */ | |
244 | for (endp = str; endp < str + nob; endp++) { | |
245 | if (!isdigit(*endp)) { | |
246 | all_numbers = false; | |
247 | break; | |
248 | } | |
249 | } | |
250 | cache = *endp; | |
251 | *endp = '\0'; | |
252 | ||
ae0246da | 253 | rc = kstrtouint(str, 10, num); |
3ad6152d JS |
254 | *endp = cache; |
255 | if (rc || !all_numbers) | |
d7e09d03 PT |
256 | return 0; |
257 | ||
d7e09d03 PT |
258 | return (*num >= min && *num <= max); |
259 | } | |
47ca6ec2 | 260 | EXPORT_SYMBOL(cfs_str2num_check); |
d7e09d03 PT |
261 | |
262 | /** | |
263 | * Parses \<range_expr\> token of the syntax. If \a bracketed is false, | |
264 | * \a src should only have a single token which can be \<number\> or \* | |
265 | * | |
266 | * \retval pointer to allocated range_expr and initialized | |
267 | * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a | |
268 | `* src parses to | |
269 | * \<number\> | | |
270 | * \<number\> '-' \<number\> | | |
271 | * \<number\> '-' \<number\> '/' \<number\> | |
272 | * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or | |
273 | * -ENOMEM will be returned. | |
274 | */ | |
31fb613a | 275 | static int |
d7e09d03 PT |
276 | cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max, |
277 | int bracketed, struct cfs_range_expr **expr) | |
278 | { | |
279 | struct cfs_range_expr *re; | |
280 | struct cfs_lstr tok; | |
281 | ||
282 | LIBCFS_ALLOC(re, sizeof(*re)); | |
15d9f520 | 283 | if (!re) |
d7e09d03 PT |
284 | return -ENOMEM; |
285 | ||
286 | if (src->ls_len == 1 && src->ls_str[0] == '*') { | |
287 | re->re_lo = min; | |
288 | re->re_hi = max; | |
289 | re->re_stride = 1; | |
290 | goto out; | |
291 | } | |
292 | ||
293 | if (cfs_str2num_check(src->ls_str, src->ls_len, | |
294 | &re->re_lo, min, max)) { | |
295 | /* <number> is parsed */ | |
296 | re->re_hi = re->re_lo; | |
297 | re->re_stride = 1; | |
298 | goto out; | |
299 | } | |
300 | ||
301 | if (!bracketed || !cfs_gettok(src, '-', &tok)) | |
302 | goto failed; | |
303 | ||
304 | if (!cfs_str2num_check(tok.ls_str, tok.ls_len, | |
305 | &re->re_lo, min, max)) | |
306 | goto failed; | |
307 | ||
308 | /* <number> - */ | |
309 | if (cfs_str2num_check(src->ls_str, src->ls_len, | |
310 | &re->re_hi, min, max)) { | |
311 | /* <number> - <number> is parsed */ | |
312 | re->re_stride = 1; | |
313 | goto out; | |
314 | } | |
315 | ||
316 | /* go to check <number> '-' <number> '/' <number> */ | |
317 | if (cfs_gettok(src, '/', &tok)) { | |
318 | if (!cfs_str2num_check(tok.ls_str, tok.ls_len, | |
319 | &re->re_hi, min, max)) | |
320 | goto failed; | |
321 | ||
322 | /* <number> - <number> / ... */ | |
323 | if (cfs_str2num_check(src->ls_str, src->ls_len, | |
324 | &re->re_stride, min, max)) { | |
325 | /* <number> - <number> / <number> is parsed */ | |
326 | goto out; | |
327 | } | |
328 | } | |
329 | ||
330 | out: | |
331 | *expr = re; | |
332 | return 0; | |
333 | ||
334 | failed: | |
335 | LIBCFS_FREE(re, sizeof(*re)); | |
336 | return -EINVAL; | |
337 | } | |
d7e09d03 | 338 | |
a1dfc93a GP |
339 | /** |
340 | * Print the range expression \a re into specified \a buffer. | |
341 | * If \a bracketed is true, expression does not need additional | |
342 | * brackets. | |
343 | * | |
344 | * \retval number of characters written | |
345 | */ | |
346 | static int | |
347 | cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr, | |
348 | bool bracketed) | |
349 | { | |
350 | int i; | |
351 | char s[] = "["; | |
352 | char e[] = "]"; | |
353 | ||
34ec7f89 OD |
354 | if (bracketed) { |
355 | s[0] = '\0'; | |
356 | e[0] = '\0'; | |
357 | } | |
a1dfc93a GP |
358 | |
359 | if (expr->re_lo == expr->re_hi) | |
360 | i = scnprintf(buffer, count, "%u", expr->re_lo); | |
361 | else if (expr->re_stride == 1) | |
362 | i = scnprintf(buffer, count, "%s%u-%u%s", | |
ae0b4833 | 363 | s, expr->re_lo, expr->re_hi, e); |
a1dfc93a GP |
364 | else |
365 | i = scnprintf(buffer, count, "%s%u-%u/%u%s", | |
ae0b4833 | 366 | s, expr->re_lo, expr->re_hi, expr->re_stride, e); |
a1dfc93a GP |
367 | return i; |
368 | } | |
369 | ||
370 | /** | |
371 | * Print a list of range expressions (\a expr_list) into specified \a buffer. | |
372 | * If the list contains several expressions, separate them with comma | |
373 | * and surround the list with brackets. | |
374 | * | |
375 | * \retval number of characters written | |
376 | */ | |
377 | int | |
378 | cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list) | |
379 | { | |
380 | struct cfs_range_expr *expr; | |
381 | int i = 0, j = 0; | |
382 | int numexprs = 0; | |
383 | ||
384 | if (count <= 0) | |
385 | return 0; | |
386 | ||
387 | list_for_each_entry(expr, &expr_list->el_exprs, re_link) | |
388 | numexprs++; | |
389 | ||
390 | if (numexprs > 1) | |
391 | i += scnprintf(buffer + i, count - i, "["); | |
392 | ||
393 | list_for_each_entry(expr, &expr_list->el_exprs, re_link) { | |
394 | if (j++ != 0) | |
395 | i += scnprintf(buffer + i, count - i, ","); | |
396 | i += cfs_range_expr_print(buffer + i, count - i, expr, | |
397 | numexprs > 1); | |
398 | } | |
399 | ||
400 | if (numexprs > 1) | |
401 | i += scnprintf(buffer + i, count - i, "]"); | |
402 | ||
403 | return i; | |
404 | } | |
405 | EXPORT_SYMBOL(cfs_expr_list_print); | |
406 | ||
d7e09d03 PT |
407 | /** |
408 | * Matches value (\a value) against ranges expression list \a expr_list. | |
409 | * | |
410 | * \retval 1 if \a value matches | |
411 | * \retval 0 otherwise | |
412 | */ | |
413 | int | |
414 | cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list) | |
415 | { | |
416 | struct cfs_range_expr *expr; | |
417 | ||
418 | list_for_each_entry(expr, &expr_list->el_exprs, re_link) { | |
419 | if (value >= expr->re_lo && value <= expr->re_hi && | |
420 | ((value - expr->re_lo) % expr->re_stride) == 0) | |
421 | return 1; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
47ca6ec2 | 426 | EXPORT_SYMBOL(cfs_expr_list_match); |
d7e09d03 PT |
427 | |
428 | /** | |
429 | * Convert express list (\a expr_list) to an array of all matched values | |
430 | * | |
431 | * \retval N N is total number of all matched values | |
432 | * \retval 0 if expression list is empty | |
433 | * \retval < 0 for failure | |
434 | */ | |
435 | int | |
436 | cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp) | |
437 | { | |
438 | struct cfs_range_expr *expr; | |
439 | __u32 *val; | |
440 | int count = 0; | |
441 | int i; | |
442 | ||
443 | list_for_each_entry(expr, &expr_list->el_exprs, re_link) { | |
444 | for (i = expr->re_lo; i <= expr->re_hi; i++) { | |
445 | if (((i - expr->re_lo) % expr->re_stride) == 0) | |
446 | count++; | |
447 | } | |
448 | } | |
449 | ||
450 | if (count == 0) /* empty expression list */ | |
451 | return 0; | |
452 | ||
453 | if (count > max) { | |
454 | CERROR("Number of values %d exceeds max allowed %d\n", | |
455 | max, count); | |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | LIBCFS_ALLOC(val, sizeof(val[0]) * count); | |
15d9f520 | 460 | if (!val) |
d7e09d03 PT |
461 | return -ENOMEM; |
462 | ||
463 | count = 0; | |
464 | list_for_each_entry(expr, &expr_list->el_exprs, re_link) { | |
465 | for (i = expr->re_lo; i <= expr->re_hi; i++) { | |
466 | if (((i - expr->re_lo) % expr->re_stride) == 0) | |
467 | val[count++] = i; | |
468 | } | |
469 | } | |
470 | ||
471 | *valpp = val; | |
472 | return count; | |
473 | } | |
474 | EXPORT_SYMBOL(cfs_expr_list_values); | |
475 | ||
476 | /** | |
477 | * Frees cfs_range_expr structures of \a expr_list. | |
478 | * | |
479 | * \retval none | |
480 | */ | |
481 | void | |
482 | cfs_expr_list_free(struct cfs_expr_list *expr_list) | |
483 | { | |
484 | while (!list_empty(&expr_list->el_exprs)) { | |
485 | struct cfs_range_expr *expr; | |
486 | ||
487 | expr = list_entry(expr_list->el_exprs.next, | |
ae0b4833 | 488 | struct cfs_range_expr, re_link); |
d7e09d03 PT |
489 | list_del(&expr->re_link); |
490 | LIBCFS_FREE(expr, sizeof(*expr)); | |
491 | } | |
492 | ||
493 | LIBCFS_FREE(expr_list, sizeof(*expr_list)); | |
494 | } | |
495 | EXPORT_SYMBOL(cfs_expr_list_free); | |
496 | ||
d7e09d03 PT |
497 | /** |
498 | * Parses \<cfs_expr_list\> token of the syntax. | |
499 | * | |
a1dfc93a GP |
500 | * \retval 0 if \a str parses to \<number\> | \<expr_list\> |
501 | * \retval -errno otherwise | |
d7e09d03 PT |
502 | */ |
503 | int | |
504 | cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max, | |
505 | struct cfs_expr_list **elpp) | |
506 | { | |
507 | struct cfs_expr_list *expr_list; | |
508 | struct cfs_range_expr *expr; | |
509 | struct cfs_lstr src; | |
510 | int rc; | |
511 | ||
512 | LIBCFS_ALLOC(expr_list, sizeof(*expr_list)); | |
15d9f520 | 513 | if (!expr_list) |
d7e09d03 PT |
514 | return -ENOMEM; |
515 | ||
516 | src.ls_str = str; | |
517 | src.ls_len = len; | |
518 | ||
519 | INIT_LIST_HEAD(&expr_list->el_exprs); | |
520 | ||
521 | if (src.ls_str[0] == '[' && | |
522 | src.ls_str[src.ls_len - 1] == ']') { | |
523 | src.ls_str++; | |
524 | src.ls_len -= 2; | |
525 | ||
526 | rc = -EINVAL; | |
15d9f520 | 527 | while (src.ls_str) { |
d7e09d03 PT |
528 | struct cfs_lstr tok; |
529 | ||
530 | if (!cfs_gettok(&src, ',', &tok)) { | |
531 | rc = -EINVAL; | |
532 | break; | |
533 | } | |
534 | ||
535 | rc = cfs_range_expr_parse(&tok, min, max, 1, &expr); | |
536 | if (rc != 0) | |
537 | break; | |
538 | ||
ae0b4833 | 539 | list_add_tail(&expr->re_link, &expr_list->el_exprs); |
d7e09d03 PT |
540 | } |
541 | } else { | |
542 | rc = cfs_range_expr_parse(&src, min, max, 0, &expr); | |
ae0b4833 OD |
543 | if (rc == 0) |
544 | list_add_tail(&expr->re_link, &expr_list->el_exprs); | |
d7e09d03 PT |
545 | } |
546 | ||
547 | if (rc != 0) | |
548 | cfs_expr_list_free(expr_list); | |
549 | else | |
550 | *elpp = expr_list; | |
551 | ||
552 | return rc; | |
553 | } | |
554 | EXPORT_SYMBOL(cfs_expr_list_parse); | |
555 | ||
556 | /** | |
557 | * Frees cfs_expr_list structures of \a list. | |
558 | * | |
559 | * For each struct cfs_expr_list structure found on \a list it frees | |
560 | * range_expr list attached to it and frees the cfs_expr_list itself. | |
561 | * | |
562 | * \retval none | |
563 | */ | |
564 | void | |
565 | cfs_expr_list_free_list(struct list_head *list) | |
566 | { | |
567 | struct cfs_expr_list *el; | |
568 | ||
569 | while (!list_empty(list)) { | |
ae0b4833 | 570 | el = list_entry(list->next, struct cfs_expr_list, el_link); |
d7e09d03 PT |
571 | list_del(&el->el_link); |
572 | cfs_expr_list_free(el); | |
573 | } | |
574 | } | |
47ca6ec2 | 575 | EXPORT_SYMBOL(cfs_expr_list_free_list); |