4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * String manipulation functions.
33 * libcfs/libcfs/libcfs_string.c
35 * Author: Nathan Rutman <nathan.rutman@sun.com>
38 #include <linux/ctype.h>
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_string.h>
42 /* Convert a text string to a bitmask */
43 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
44 int *oldmask, int minmask, int allmask, int defmask)
48 int newmask = minmask, i, len, found = 0;
51 /* <str> must be a list of tokens separated by whitespace or comma,
52 * and optionally an operator ('+' or '-'). If an operator
53 * appears first in <str>, '*oldmask' is used as the starting point
54 * (relative), otherwise minmask is used (absolute). An operator
55 * applies to all following tokens up to the next operator.
58 while (isspace(*str) || *str == ',')
62 if (*str == '+' || *str == '-') {
65 /* only if first token is relative */
69 if (*str == 0) /* trailing op */
73 /* find token length */
74 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
75 str[len] != '+' && str[len] != '-' && str[len] != ',';
80 for (i = 0; i < 32; i++) {
81 debugstr = bit2str(i);
82 if (debugstr != NULL &&
83 strlen(debugstr) == len &&
84 strncasecmp(str, debugstr, len) == 0) {
93 if (!found && len == 3 &&
94 (strncasecmp(str, "ALL", len) == 0)) {
101 if (!found && strcasecmp(str, "DEFAULT") == 0) {
103 newmask = (newmask & ~defmask) | minmask;
111 CWARN("unknown mask '%.*s'.\n"
112 "mask usage: [+|-]<all|type> ...\n", len, str);
121 EXPORT_SYMBOL(cfs_str2mask);
124 * Extracts tokens from strings.
126 * Looks for \a delim in string \a next, sets \a res to point to
127 * substring before the delimiter, sets \a next right after the found
130 * \retval 1 if \a res points to a string of non-whitespace characters
131 * \retval 0 otherwise
134 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
138 if (next->ls_str == NULL)
141 /* skip leading white spaces */
142 while (next->ls_len) {
143 if (!isspace(*next->ls_str))
149 if (next->ls_len == 0) /* whitespaces only */
152 if (*next->ls_str == delim) {
153 /* first non-writespace is the delimiter */
157 res->ls_str = next->ls_str;
158 end = memchr(next->ls_str, delim, next->ls_len);
160 /* there is no the delimeter in the string */
161 end = next->ls_str + next->ls_len;
165 next->ls_str = end + 1;
166 next->ls_len -= (end - res->ls_str + 1);
169 /* skip ending whitespaces */
170 while (--end != res->ls_str) {
175 res->ls_len = end - res->ls_str + 1;
178 EXPORT_SYMBOL(cfs_gettok);
181 * Converts string to integer.
183 * Accepts decimal and hexadecimal number recordings.
185 * \retval 1 if first \a nob chars of \a str convert to decimal or
186 * hexadecimal integer in the range [\a min, \a max]
187 * \retval 0 otherwise
190 cfs_str2num_check(char *str, int nob, unsigned *num,
191 unsigned min, unsigned max)
193 bool all_numbers = true;
200 * kstrouint can only handle strings composed
201 * of only numbers. We need to scan the string
202 * passed in for the first non-digit character
203 * and end the string at that location. If we
204 * don't find any non-digit character we still
205 * need to place a '\0' at position len since
206 * we are not interested in the rest of the
207 * string which is longer than len in size.
208 * After we are done the character at the
209 * position we placed '\0' must be restored.
211 len = min((int)strlen(endp), nob);
212 for (; endp < str + len; endp++) {
213 if (!isxdigit(*endp) && *endp != '-' &&
220 /* Eat trailing space */
221 if (!all_numbers && isspace(*endp)) {
229 rc = kstrtouint(str, 0, num);
231 if (rc || !all_numbers)
234 return (*num >= min && *num <= max);
236 EXPORT_SYMBOL(cfs_str2num_check);
239 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
240 * \a src should only have a single token which can be \<number\> or \*
242 * \retval pointer to allocated range_expr and initialized
243 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
246 * \<number\> '-' \<number\> |
247 * \<number\> '-' \<number\> '/' \<number\>
248 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
249 * -ENOMEM will be returned.
252 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
253 int bracketed, struct cfs_range_expr **expr)
255 struct cfs_range_expr *re;
258 LIBCFS_ALLOC(re, sizeof(*re));
262 if (src->ls_len == 1 && src->ls_str[0] == '*') {
269 if (cfs_str2num_check(src->ls_str, src->ls_len,
270 &re->re_lo, min, max)) {
271 /* <number> is parsed */
272 re->re_hi = re->re_lo;
277 if (!bracketed || !cfs_gettok(src, '-', &tok))
280 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
281 &re->re_lo, min, max))
285 if (cfs_str2num_check(src->ls_str, src->ls_len,
286 &re->re_hi, min, max)) {
287 /* <number> - <number> is parsed */
292 /* go to check <number> '-' <number> '/' <number> */
293 if (cfs_gettok(src, '/', &tok)) {
294 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
295 &re->re_hi, min, max))
298 /* <number> - <number> / ... */
299 if (cfs_str2num_check(src->ls_str, src->ls_len,
300 &re->re_stride, min, max)) {
301 /* <number> - <number> / <number> is parsed */
311 LIBCFS_FREE(re, sizeof(*re));
316 * Print the range expression \a re into specified \a buffer.
317 * If \a bracketed is true, expression does not need additional
320 * \retval number of characters written
323 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
333 if (expr->re_lo == expr->re_hi)
334 i = scnprintf(buffer, count, "%u", expr->re_lo);
335 else if (expr->re_stride == 1)
336 i = scnprintf(buffer, count, "%s%u-%u%s",
337 s, expr->re_lo, expr->re_hi, e);
339 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
340 s, expr->re_lo, expr->re_hi,
346 * Print a list of range expressions (\a expr_list) into specified \a buffer.
347 * If the list contains several expressions, separate them with comma
348 * and surround the list with brackets.
350 * \retval number of characters written
353 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
355 struct cfs_range_expr *expr;
362 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
366 i += scnprintf(buffer + i, count - i, "[");
368 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
370 i += scnprintf(buffer + i, count - i, ",");
371 i += cfs_range_expr_print(buffer + i, count - i, expr,
376 i += scnprintf(buffer + i, count - i, "]");
380 EXPORT_SYMBOL(cfs_expr_list_print);
383 * Matches value (\a value) against ranges expression list \a expr_list.
385 * \retval 1 if \a value matches
386 * \retval 0 otherwise
389 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
391 struct cfs_range_expr *expr;
393 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
394 if (value >= expr->re_lo && value <= expr->re_hi &&
395 ((value - expr->re_lo) % expr->re_stride) == 0)
401 EXPORT_SYMBOL(cfs_expr_list_match);
404 * Convert express list (\a expr_list) to an array of all matched values
406 * \retval N N is total number of all matched values
407 * \retval 0 if expression list is empty
408 * \retval < 0 for failure
411 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
413 struct cfs_range_expr *expr;
418 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
419 for (i = expr->re_lo; i <= expr->re_hi; i++) {
420 if (((i - expr->re_lo) % expr->re_stride) == 0)
425 if (count == 0) /* empty expression list */
429 CERROR("Number of values %d exceeds max allowed %d\n",
434 CFS_ALLOC_PTR_ARRAY(val, count);
439 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
440 for (i = expr->re_lo; i <= expr->re_hi; i++) {
441 if (((i - expr->re_lo) % expr->re_stride) == 0)
449 EXPORT_SYMBOL(cfs_expr_list_values);
452 cfs_expr_list_values_free(__u32 *values, int num)
454 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
455 * by OBD_FREE() if it's called by module other than libcfs & LNet,
456 * otherwise we will see fake memory leak */
457 CFS_FREE_PTR_ARRAY(values, num);
459 EXPORT_SYMBOL(cfs_expr_list_values_free);
462 * Frees cfs_range_expr structures of \a expr_list.
467 cfs_expr_list_free(struct cfs_expr_list *expr_list)
469 while (!list_empty(&expr_list->el_exprs)) {
470 struct cfs_range_expr *expr;
472 expr = list_first_entry(&expr_list->el_exprs,
473 struct cfs_range_expr, re_link);
474 list_del(&expr->re_link);
475 LIBCFS_FREE(expr, sizeof(*expr));
478 LIBCFS_FREE(expr_list, sizeof(*expr_list));
480 EXPORT_SYMBOL(cfs_expr_list_free);
483 * Parses \<cfs_expr_list\> token of the syntax.
485 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
486 * \retval -errno otherwise
489 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
490 struct cfs_expr_list **elpp)
492 struct cfs_expr_list *expr_list;
493 struct cfs_range_expr *expr;
497 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
498 if (expr_list == NULL)
504 INIT_LIST_HEAD(&expr_list->el_exprs);
506 if (src.ls_str[0] == '[' &&
507 src.ls_str[src.ls_len - 1] == ']') {
512 while (src.ls_str != NULL) {
515 if (!cfs_gettok(&src, ',', &tok)) {
520 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
524 list_add_tail(&expr->re_link, &expr_list->el_exprs);
527 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
529 list_add_tail(&expr->re_link, &expr_list->el_exprs);
533 cfs_expr_list_free(expr_list);
539 EXPORT_SYMBOL(cfs_expr_list_parse);
542 * Frees cfs_expr_list structures of \a list.
544 * For each struct cfs_expr_list structure found on \a list it frees
545 * range_expr list attached to it and frees the cfs_expr_list itself.
550 cfs_expr_list_free_list(struct list_head *list)
552 struct cfs_expr_list *el;
554 while (!list_empty(list)) {
555 el = list_first_entry(list,
556 struct cfs_expr_list, el_link);
557 list_del(&el->el_link);
558 cfs_expr_list_free(el);
561 EXPORT_SYMBOL(cfs_expr_list_free_list);