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, 2015, 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 <libcfs/libcfs.h>
41 char *cfs_strrstr(const char *haystack, const char *needle)
45 if (unlikely(haystack == NULL || needle == NULL))
48 if (strlen(needle) == 1)
49 return strrchr(haystack, needle[0]);
51 ptr = strstr(haystack, needle);
56 tmp = strstr(&ptr[1], needle);
66 EXPORT_SYMBOL(cfs_strrstr);
68 /* Convert a text string to a bitmask */
69 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
70 int *oldmask, int minmask, int allmask)
74 int newmask = minmask, i, len, found = 0;
77 /* <str> must be a list of tokens separated by whitespace
78 * and optionally an operator ('+' or '-'). If an operator
79 * appears first in <str>, '*oldmask' is used as the starting point
80 * (relative), otherwise minmask is used (absolute). An operator
81 * applies to all following tokens up to the next operator. */
87 if (*str == '+' || *str == '-') {
90 /* only if first token is relative */
94 if (*str == 0) /* trailing op */
98 /* find token length */
99 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
100 str[len] != '+' && str[len] != '-'; len++);
104 for (i = 0; i < 32; i++) {
105 debugstr = bit2str(i);
106 if (debugstr != NULL &&
107 strlen(debugstr) == len &&
108 strncasecmp(str, debugstr, len) == 0) {
110 newmask &= ~(1 << i);
117 if (!found && len == 3 &&
118 (strncasecmp(str, "ALL", len) == 0)) {
126 CWARN("unknown mask '%.*s'.\n"
127 "mask usage: [+|-]<all|type> ...\n", len, str);
136 EXPORT_SYMBOL(cfs_str2mask);
138 /* get the first string out of @str */
139 char *cfs_firststr(char *str, size_t size)
144 /* trim leading spaces */
145 while (i < size && *str && isspace(*str)) {
150 /* string with all spaces */
155 while (i < size && *end != '\0' && !isspace(*end)) {
164 EXPORT_SYMBOL(cfs_firststr);
167 cfs_trimwhite(char *str)
171 while (isspace(*str))
174 end = str + strlen(str);
176 if (!isspace(end[-1]))
184 EXPORT_SYMBOL(cfs_trimwhite);
187 * Extracts tokens from strings.
189 * Looks for \a delim in string \a next, sets \a res to point to
190 * substring before the delimiter, sets \a next right after the found
193 * \retval 1 if \a res points to a string of non-whitespace characters
194 * \retval 0 otherwise
197 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
201 if (next->ls_str == NULL)
204 /* skip leading white spaces */
205 while (next->ls_len) {
206 if (!isspace(*next->ls_str))
212 if (next->ls_len == 0) /* whitespaces only */
215 if (*next->ls_str == delim) {
216 /* first non-writespace is the delimiter */
220 res->ls_str = next->ls_str;
221 end = memchr(next->ls_str, delim, next->ls_len);
223 /* there is no the delimeter in the string */
224 end = next->ls_str + next->ls_len;
227 next->ls_str = end + 1;
228 next->ls_len -= (end - res->ls_str + 1);
231 /* skip ending whitespaces */
232 while (--end != res->ls_str) {
237 res->ls_len = end - res->ls_str + 1;
240 EXPORT_SYMBOL(cfs_gettok);
243 * Converts string to integer.
245 * Accepts decimal and hexadecimal number recordings.
247 * \retval 1 if first \a nob chars of \a str convert to decimal or
248 * hexadecimal integer in the range [\a min, \a max]
249 * \retval 0 otherwise
252 cfs_str2num_check(char *str, int nob, unsigned *num,
253 unsigned min, unsigned max)
257 *num = simple_strtoul(str, &endp, 0);
261 for (; endp < str + nob; endp++) {
266 return (*num >= min && *num <= max);
268 EXPORT_SYMBOL(cfs_str2num_check);
271 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
272 * \a src should only have a single token which can be \<number\> or \*
274 * \retval pointer to allocated range_expr and initialized
275 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
278 * \<number\> '-' \<number\> |
279 * \<number\> '-' \<number\> '/' \<number\>
280 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
281 * -ENOMEM will be returned.
284 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
285 int bracketed, struct cfs_range_expr **expr)
287 struct cfs_range_expr *re;
290 LIBCFS_ALLOC(re, sizeof(*re));
294 if (src->ls_len == 1 && src->ls_str[0] == '*') {
301 if (cfs_str2num_check(src->ls_str, src->ls_len,
302 &re->re_lo, min, max)) {
303 /* <number> is parsed */
304 re->re_hi = re->re_lo;
309 if (!bracketed || !cfs_gettok(src, '-', &tok))
312 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
313 &re->re_lo, min, max))
317 if (cfs_str2num_check(src->ls_str, src->ls_len,
318 &re->re_hi, min, max)) {
319 /* <number> - <number> is parsed */
324 /* go to check <number> '-' <number> '/' <number> */
325 if (cfs_gettok(src, '/', &tok)) {
326 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
327 &re->re_hi, min, max))
330 /* <number> - <number> / ... */
331 if (cfs_str2num_check(src->ls_str, src->ls_len,
332 &re->re_stride, min, max)) {
333 /* <number> - <number> / <number> is parsed */
343 LIBCFS_FREE(re, sizeof(*re));
348 * Print the range expression \a re into specified \a buffer.
349 * If \a bracketed is true, expression does not need additional
352 * \retval number of characters written
355 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
365 if (expr->re_lo == expr->re_hi)
366 i = scnprintf(buffer, count, "%u", expr->re_lo);
367 else if (expr->re_stride == 1)
368 i = scnprintf(buffer, count, "%s%u-%u%s",
369 s, expr->re_lo, expr->re_hi, e);
371 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
372 s, expr->re_lo, expr->re_hi,
378 * Print a list of range expressions (\a expr_list) into specified \a buffer.
379 * If the list contains several expressions, separate them with comma
380 * and surround the list with brackets.
382 * \retval number of characters written
385 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
387 struct cfs_range_expr *expr;
394 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
398 i += scnprintf(buffer + i, count - i, "[");
400 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
402 i += scnprintf(buffer + i, count - i, ",");
403 i += cfs_range_expr_print(buffer + i, count - i, expr,
408 i += scnprintf(buffer + i, count - i, "]");
412 EXPORT_SYMBOL(cfs_expr_list_print);
415 * Matches value (\a value) against ranges expression list \a expr_list.
417 * \retval 1 if \a value matches
418 * \retval 0 otherwise
421 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
423 struct cfs_range_expr *expr;
425 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
426 if (value >= expr->re_lo && value <= expr->re_hi &&
427 ((value - expr->re_lo) % expr->re_stride) == 0)
433 EXPORT_SYMBOL(cfs_expr_list_match);
436 * Convert express list (\a expr_list) to an array of all matched values
438 * \retval N N is total number of all matched values
439 * \retval 0 if expression list is empty
440 * \retval < 0 for failure
443 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
445 struct cfs_range_expr *expr;
450 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
451 for (i = expr->re_lo; i <= expr->re_hi; i++) {
452 if (((i - expr->re_lo) % expr->re_stride) == 0)
457 if (count == 0) /* empty expression list */
461 CERROR("Number of values %d exceeds max allowed %d\n",
466 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
471 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
472 for (i = expr->re_lo; i <= expr->re_hi; i++) {
473 if (((i - expr->re_lo) % expr->re_stride) == 0)
481 EXPORT_SYMBOL(cfs_expr_list_values);
484 cfs_expr_list_values_free(__u32 *values, int num)
486 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
487 * by OBD_FREE() if it's called by module other than libcfs & LNet,
488 * otherwise we will see fake memory leak */
489 LIBCFS_FREE(values, num * sizeof(values[0]));
491 EXPORT_SYMBOL(cfs_expr_list_values_free);
494 * Frees cfs_range_expr structures of \a expr_list.
499 cfs_expr_list_free(struct cfs_expr_list *expr_list)
501 while (!list_empty(&expr_list->el_exprs)) {
502 struct cfs_range_expr *expr;
504 expr = list_entry(expr_list->el_exprs.next,
505 struct cfs_range_expr, re_link);
506 list_del(&expr->re_link);
507 LIBCFS_FREE(expr, sizeof(*expr));
510 LIBCFS_FREE(expr_list, sizeof(*expr_list));
512 EXPORT_SYMBOL(cfs_expr_list_free);
515 * Parses \<cfs_expr_list\> token of the syntax.
517 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
518 * \retval -errno otherwise
521 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
522 struct cfs_expr_list **elpp)
524 struct cfs_expr_list *expr_list;
525 struct cfs_range_expr *expr;
529 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
530 if (expr_list == NULL)
536 INIT_LIST_HEAD(&expr_list->el_exprs);
538 if (src.ls_str[0] == '[' &&
539 src.ls_str[src.ls_len - 1] == ']') {
544 while (src.ls_str != NULL) {
547 if (!cfs_gettok(&src, ',', &tok)) {
552 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
556 list_add_tail(&expr->re_link,
557 &expr_list->el_exprs);
560 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
562 list_add_tail(&expr->re_link,
563 &expr_list->el_exprs);
568 cfs_expr_list_free(expr_list);
574 EXPORT_SYMBOL(cfs_expr_list_parse);
577 * Frees cfs_expr_list structures of \a list.
579 * For each struct cfs_expr_list structure found on \a list it frees
580 * range_expr list attached to it and frees the cfs_expr_list itself.
585 cfs_expr_list_free_list(struct list_head *list)
587 struct cfs_expr_list *el;
589 while (!list_empty(list)) {
590 el = list_entry(list->next,
591 struct cfs_expr_list, el_link);
592 list_del(&el->el_link);
593 cfs_expr_list_free(el);
596 EXPORT_SYMBOL(cfs_expr_list_free_list);