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);
141 /* get the first string out of @str */
142 char *cfs_firststr(char *str, size_t size)
147 /* trim leading spaces */
148 while (i < size && *str && isspace(*str)) {
153 /* string with all spaces */
158 while (i < size && *end != '\0' && !isspace(*end)) {
167 EXPORT_SYMBOL(cfs_firststr);
170 * Extracts tokens from strings.
172 * Looks for \a delim in string \a next, sets \a res to point to
173 * substring before the delimiter, sets \a next right after the found
176 * \retval 1 if \a res points to a string of non-whitespace characters
177 * \retval 0 otherwise
180 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
184 if (next->ls_str == NULL)
187 /* skip leading white spaces */
188 while (next->ls_len) {
189 if (!isspace(*next->ls_str))
195 if (next->ls_len == 0) /* whitespaces only */
198 if (*next->ls_str == delim) {
199 /* first non-writespace is the delimiter */
203 res->ls_str = next->ls_str;
204 end = memchr(next->ls_str, delim, next->ls_len);
206 /* there is no the delimeter in the string */
207 end = next->ls_str + next->ls_len;
210 next->ls_str = end + 1;
211 next->ls_len -= (end - res->ls_str + 1);
214 /* skip ending whitespaces */
215 while (--end != res->ls_str) {
220 res->ls_len = end - res->ls_str + 1;
223 EXPORT_SYMBOL(cfs_gettok);
226 * Converts string to integer.
228 * Accepts decimal and hexadecimal number recordings.
230 * \retval 1 if first \a nob chars of \a str convert to decimal or
231 * hexadecimal integer in the range [\a min, \a max]
232 * \retval 0 otherwise
235 cfs_str2num_check(char *str, int nob, unsigned *num,
236 unsigned min, unsigned max)
238 bool all_numbers = true;
245 * kstrouint can only handle strings composed
246 * of only numbers. We need to scan the string
247 * passed in for the first non-digit character
248 * and end the string at that location. If we
249 * don't find any non-digit character we still
250 * need to place a '\0' at position len since
251 * we are not interested in the rest of the
252 * string which is longer than len in size.
253 * After we are done the character at the
254 * position we placed '\0' must be restored.
256 len = min((int)strlen(endp), nob);
257 for (; endp < str + len; endp++) {
258 if (!isxdigit(*endp) && *endp != '-' &&
265 /* Eat trailing space */
266 if (!all_numbers && isspace(*endp)) {
274 rc = kstrtouint(str, 0, num);
276 if (rc || !all_numbers)
279 return (*num >= min && *num <= max);
281 EXPORT_SYMBOL(cfs_str2num_check);
284 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
285 * \a src should only have a single token which can be \<number\> or \*
287 * \retval pointer to allocated range_expr and initialized
288 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
291 * \<number\> '-' \<number\> |
292 * \<number\> '-' \<number\> '/' \<number\>
293 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
294 * -ENOMEM will be returned.
297 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
298 int bracketed, struct cfs_range_expr **expr)
300 struct cfs_range_expr *re;
303 LIBCFS_ALLOC(re, sizeof(*re));
307 if (src->ls_len == 1 && src->ls_str[0] == '*') {
314 if (cfs_str2num_check(src->ls_str, src->ls_len,
315 &re->re_lo, min, max)) {
316 /* <number> is parsed */
317 re->re_hi = re->re_lo;
322 if (!bracketed || !cfs_gettok(src, '-', &tok))
325 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
326 &re->re_lo, min, max))
330 if (cfs_str2num_check(src->ls_str, src->ls_len,
331 &re->re_hi, min, max)) {
332 /* <number> - <number> is parsed */
337 /* go to check <number> '-' <number> '/' <number> */
338 if (cfs_gettok(src, '/', &tok)) {
339 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
340 &re->re_hi, min, max))
343 /* <number> - <number> / ... */
344 if (cfs_str2num_check(src->ls_str, src->ls_len,
345 &re->re_stride, min, max)) {
346 /* <number> - <number> / <number> is parsed */
356 LIBCFS_FREE(re, sizeof(*re));
361 * Print the range expression \a re into specified \a buffer.
362 * If \a bracketed is true, expression does not need additional
365 * \retval number of characters written
368 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
378 if (expr->re_lo == expr->re_hi)
379 i = scnprintf(buffer, count, "%u", expr->re_lo);
380 else if (expr->re_stride == 1)
381 i = scnprintf(buffer, count, "%s%u-%u%s",
382 s, expr->re_lo, expr->re_hi, e);
384 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
385 s, expr->re_lo, expr->re_hi,
391 * Print a list of range expressions (\a expr_list) into specified \a buffer.
392 * If the list contains several expressions, separate them with comma
393 * and surround the list with brackets.
395 * \retval number of characters written
398 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
400 struct cfs_range_expr *expr;
407 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
411 i += scnprintf(buffer + i, count - i, "[");
413 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
415 i += scnprintf(buffer + i, count - i, ",");
416 i += cfs_range_expr_print(buffer + i, count - i, expr,
421 i += scnprintf(buffer + i, count - i, "]");
425 EXPORT_SYMBOL(cfs_expr_list_print);
428 * Matches value (\a value) against ranges expression list \a expr_list.
430 * \retval 1 if \a value matches
431 * \retval 0 otherwise
434 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
436 struct cfs_range_expr *expr;
438 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
439 if (value >= expr->re_lo && value <= expr->re_hi &&
440 ((value - expr->re_lo) % expr->re_stride) == 0)
446 EXPORT_SYMBOL(cfs_expr_list_match);
449 * Convert express list (\a expr_list) to an array of all matched values
451 * \retval N N is total number of all matched values
452 * \retval 0 if expression list is empty
453 * \retval < 0 for failure
456 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
458 struct cfs_range_expr *expr;
463 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
464 for (i = expr->re_lo; i <= expr->re_hi; i++) {
465 if (((i - expr->re_lo) % expr->re_stride) == 0)
470 if (count == 0) /* empty expression list */
474 CERROR("Number of values %d exceeds max allowed %d\n",
479 CFS_ALLOC_PTR_ARRAY(val, count);
484 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
485 for (i = expr->re_lo; i <= expr->re_hi; i++) {
486 if (((i - expr->re_lo) % expr->re_stride) == 0)
494 EXPORT_SYMBOL(cfs_expr_list_values);
497 cfs_expr_list_values_free(__u32 *values, int num)
499 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
500 * by OBD_FREE() if it's called by module other than libcfs & LNet,
501 * otherwise we will see fake memory leak */
502 CFS_FREE_PTR_ARRAY(values, num);
504 EXPORT_SYMBOL(cfs_expr_list_values_free);
507 * Frees cfs_range_expr structures of \a expr_list.
512 cfs_expr_list_free(struct cfs_expr_list *expr_list)
514 while (!list_empty(&expr_list->el_exprs)) {
515 struct cfs_range_expr *expr;
517 expr = list_entry(expr_list->el_exprs.next,
518 struct cfs_range_expr, re_link);
519 list_del(&expr->re_link);
520 LIBCFS_FREE(expr, sizeof(*expr));
523 LIBCFS_FREE(expr_list, sizeof(*expr_list));
525 EXPORT_SYMBOL(cfs_expr_list_free);
528 * Parses \<cfs_expr_list\> token of the syntax.
530 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
531 * \retval -errno otherwise
534 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
535 struct cfs_expr_list **elpp)
537 struct cfs_expr_list *expr_list;
538 struct cfs_range_expr *expr;
542 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
543 if (expr_list == NULL)
549 INIT_LIST_HEAD(&expr_list->el_exprs);
551 if (src.ls_str[0] == '[' &&
552 src.ls_str[src.ls_len - 1] == ']') {
557 while (src.ls_str != NULL) {
560 if (!cfs_gettok(&src, ',', &tok)) {
565 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
569 list_add_tail(&expr->re_link, &expr_list->el_exprs);
572 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
574 list_add_tail(&expr->re_link, &expr_list->el_exprs);
578 cfs_expr_list_free(expr_list);
584 EXPORT_SYMBOL(cfs_expr_list_parse);
587 * Frees cfs_expr_list structures of \a list.
589 * For each struct cfs_expr_list structure found on \a list it frees
590 * range_expr list attached to it and frees the cfs_expr_list itself.
595 cfs_expr_list_free_list(struct list_head *list)
597 struct cfs_expr_list *el;
599 while (!list_empty(list)) {
600 el = list_entry(list->next,
601 struct cfs_expr_list, el_link);
602 list_del(&el->el_link);
603 cfs_expr_list_free(el);
606 EXPORT_SYMBOL(cfs_expr_list_free_list);