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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * String manipulation functions.
34 * libcfs/libcfs/libcfs_string.c
36 * Author: Nathan Rutman <nathan.rutman@sun.com>
39 #include <linux/ctype.h>
40 #include <libcfs/libcfs.h>
41 #include <libcfs/libcfs_string.h>
43 char *cfs_strrstr(const char *haystack, const char *needle)
47 if (unlikely(haystack == NULL || needle == NULL))
50 if (strlen(needle) == 1)
51 return strrchr(haystack, needle[0]);
53 ptr = strstr(haystack, needle);
58 tmp = strstr(&ptr[1], needle);
68 EXPORT_SYMBOL(cfs_strrstr);
70 /* Convert a text string to a bitmask */
71 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
72 int *oldmask, int minmask, int allmask)
76 int newmask = minmask, i, len, found = 0;
79 /* <str> must be a list of tokens separated by whitespace
80 * and optionally an operator ('+' or '-'). If an operator
81 * appears first in <str>, '*oldmask' is used as the starting point
82 * (relative), otherwise minmask is used (absolute). An operator
83 * applies to all following tokens up to the next operator.
90 if (*str == '+' || *str == '-') {
93 /* only if first token is relative */
97 if (*str == 0) /* trailing op */
101 /* find token length */
102 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
103 str[len] != '+' && str[len] != '-'; len++);
107 for (i = 0; i < 32; i++) {
108 debugstr = bit2str(i);
109 if (debugstr != NULL &&
110 strlen(debugstr) == len &&
111 strncasecmp(str, debugstr, len) == 0) {
120 if (!found && len == 3 &&
121 (strncasecmp(str, "ALL", len) == 0)) {
129 CWARN("unknown mask '%.*s'.\n"
130 "mask usage: [+|-]<all|type> ...\n", len, str);
139 EXPORT_SYMBOL(cfs_str2mask);
142 * Extracts tokens from strings.
144 * Looks for \a delim in string \a next, sets \a res to point to
145 * substring before the delimiter, sets \a next right after the found
148 * \retval 1 if \a res points to a string of non-whitespace characters
149 * \retval 0 otherwise
152 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
156 if (next->ls_str == NULL)
159 /* skip leading white spaces */
160 while (next->ls_len) {
161 if (!isspace(*next->ls_str))
167 if (next->ls_len == 0) /* whitespaces only */
170 if (*next->ls_str == delim) {
171 /* first non-writespace is the delimiter */
175 res->ls_str = next->ls_str;
176 end = memchr(next->ls_str, delim, next->ls_len);
178 /* there is no the delimeter in the string */
179 end = next->ls_str + next->ls_len;
182 next->ls_str = end + 1;
183 next->ls_len -= (end - res->ls_str + 1);
186 /* skip ending whitespaces */
187 while (--end != res->ls_str) {
192 res->ls_len = end - res->ls_str + 1;
195 EXPORT_SYMBOL(cfs_gettok);
198 * Converts string to integer.
200 * Accepts decimal and hexadecimal number recordings.
202 * \retval 1 if first \a nob chars of \a str convert to decimal or
203 * hexadecimal integer in the range [\a min, \a max]
204 * \retval 0 otherwise
207 cfs_str2num_check(char *str, int nob, unsigned *num,
208 unsigned min, unsigned max)
210 bool all_numbers = true;
217 * kstrouint can only handle strings composed
218 * of only numbers. We need to scan the string
219 * passed in for the first non-digit character
220 * and end the string at that location. If we
221 * don't find any non-digit character we still
222 * need to place a '\0' at position len since
223 * we are not interested in the rest of the
224 * string which is longer than len in size.
225 * After we are done the character at the
226 * position we placed '\0' must be restored.
228 len = min((int)strlen(endp), nob);
229 for (; endp < str + len; endp++) {
230 if (!isxdigit(*endp) && *endp != '-' &&
237 /* Eat trailing space */
238 if (!all_numbers && isspace(*endp)) {
246 rc = kstrtouint(str, 0, num);
248 if (rc || !all_numbers)
251 return (*num >= min && *num <= max);
253 EXPORT_SYMBOL(cfs_str2num_check);
256 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
257 * \a src should only have a single token which can be \<number\> or \*
259 * \retval pointer to allocated range_expr and initialized
260 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
263 * \<number\> '-' \<number\> |
264 * \<number\> '-' \<number\> '/' \<number\>
265 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
266 * -ENOMEM will be returned.
269 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
270 int bracketed, struct cfs_range_expr **expr)
272 struct cfs_range_expr *re;
275 LIBCFS_ALLOC(re, sizeof(*re));
279 if (src->ls_len == 1 && src->ls_str[0] == '*') {
286 if (cfs_str2num_check(src->ls_str, src->ls_len,
287 &re->re_lo, min, max)) {
288 /* <number> is parsed */
289 re->re_hi = re->re_lo;
294 if (!bracketed || !cfs_gettok(src, '-', &tok))
297 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
298 &re->re_lo, min, max))
302 if (cfs_str2num_check(src->ls_str, src->ls_len,
303 &re->re_hi, min, max)) {
304 /* <number> - <number> is parsed */
309 /* go to check <number> '-' <number> '/' <number> */
310 if (cfs_gettok(src, '/', &tok)) {
311 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
312 &re->re_hi, min, max))
315 /* <number> - <number> / ... */
316 if (cfs_str2num_check(src->ls_str, src->ls_len,
317 &re->re_stride, min, max)) {
318 /* <number> - <number> / <number> is parsed */
328 LIBCFS_FREE(re, sizeof(*re));
333 * Print the range expression \a re into specified \a buffer.
334 * If \a bracketed is true, expression does not need additional
337 * \retval number of characters written
340 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
350 if (expr->re_lo == expr->re_hi)
351 i = scnprintf(buffer, count, "%u", expr->re_lo);
352 else if (expr->re_stride == 1)
353 i = scnprintf(buffer, count, "%s%u-%u%s",
354 s, expr->re_lo, expr->re_hi, e);
356 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
357 s, expr->re_lo, expr->re_hi,
363 * Print a list of range expressions (\a expr_list) into specified \a buffer.
364 * If the list contains several expressions, separate them with comma
365 * and surround the list with brackets.
367 * \retval number of characters written
370 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
372 struct cfs_range_expr *expr;
379 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
383 i += scnprintf(buffer + i, count - i, "[");
385 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
387 i += scnprintf(buffer + i, count - i, ",");
388 i += cfs_range_expr_print(buffer + i, count - i, expr,
393 i += scnprintf(buffer + i, count - i, "]");
397 EXPORT_SYMBOL(cfs_expr_list_print);
400 * Matches value (\a value) against ranges expression list \a expr_list.
402 * \retval 1 if \a value matches
403 * \retval 0 otherwise
406 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
408 struct cfs_range_expr *expr;
410 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
411 if (value >= expr->re_lo && value <= expr->re_hi &&
412 ((value - expr->re_lo) % expr->re_stride) == 0)
418 EXPORT_SYMBOL(cfs_expr_list_match);
421 * Convert express list (\a expr_list) to an array of all matched values
423 * \retval N N is total number of all matched values
424 * \retval 0 if expression list is empty
425 * \retval < 0 for failure
428 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
430 struct cfs_range_expr *expr;
435 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
436 for (i = expr->re_lo; i <= expr->re_hi; i++) {
437 if (((i - expr->re_lo) % expr->re_stride) == 0)
442 if (count == 0) /* empty expression list */
446 CERROR("Number of values %d exceeds max allowed %d\n",
451 CFS_ALLOC_PTR_ARRAY(val, count);
456 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
457 for (i = expr->re_lo; i <= expr->re_hi; i++) {
458 if (((i - expr->re_lo) % expr->re_stride) == 0)
466 EXPORT_SYMBOL(cfs_expr_list_values);
469 cfs_expr_list_values_free(__u32 *values, int num)
471 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
472 * by OBD_FREE() if it's called by module other than libcfs & LNet,
473 * otherwise we will see fake memory leak */
474 CFS_FREE_PTR_ARRAY(values, num);
476 EXPORT_SYMBOL(cfs_expr_list_values_free);
479 * Frees cfs_range_expr structures of \a expr_list.
484 cfs_expr_list_free(struct cfs_expr_list *expr_list)
486 while (!list_empty(&expr_list->el_exprs)) {
487 struct cfs_range_expr *expr;
489 expr = list_entry(expr_list->el_exprs.next,
490 struct cfs_range_expr, re_link);
491 list_del(&expr->re_link);
492 LIBCFS_FREE(expr, sizeof(*expr));
495 LIBCFS_FREE(expr_list, sizeof(*expr_list));
497 EXPORT_SYMBOL(cfs_expr_list_free);
500 * Parses \<cfs_expr_list\> token of the syntax.
502 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
503 * \retval -errno otherwise
506 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
507 struct cfs_expr_list **elpp)
509 struct cfs_expr_list *expr_list;
510 struct cfs_range_expr *expr;
514 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
515 if (expr_list == NULL)
521 INIT_LIST_HEAD(&expr_list->el_exprs);
523 if (src.ls_str[0] == '[' &&
524 src.ls_str[src.ls_len - 1] == ']') {
529 while (src.ls_str != NULL) {
532 if (!cfs_gettok(&src, ',', &tok)) {
537 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
541 list_add_tail(&expr->re_link, &expr_list->el_exprs);
544 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
546 list_add_tail(&expr->re_link, &expr_list->el_exprs);
550 cfs_expr_list_free(expr_list);
556 EXPORT_SYMBOL(cfs_expr_list_parse);
559 * Frees cfs_expr_list structures of \a list.
561 * For each struct cfs_expr_list structure found on \a list it frees
562 * range_expr list attached to it and frees the cfs_expr_list itself.
567 cfs_expr_list_free_list(struct list_head *list)
569 struct cfs_expr_list *el;
571 while (!list_empty(list)) {
572 el = list_entry(list->next,
573 struct cfs_expr_list, el_link);
574 list_del(&el->el_link);
575 cfs_expr_list_free(el);
578 EXPORT_SYMBOL(cfs_expr_list_free_list);