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. */
89 if (*str == '+' || *str == '-') {
92 /* only if first token is relative */
96 if (*str == 0) /* trailing op */
100 /* find token length */
101 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
102 str[len] != '+' && str[len] != '-'; len++);
106 for (i = 0; i < 32; i++) {
107 debugstr = bit2str(i);
108 if (debugstr != NULL &&
109 strlen(debugstr) == len &&
110 strncasecmp(str, debugstr, len) == 0) {
112 newmask &= ~(1 << i);
119 if (!found && len == 3 &&
120 (strncasecmp(str, "ALL", len) == 0)) {
128 CWARN("unknown mask '%.*s'.\n"
129 "mask usage: [+|-]<all|type> ...\n", len, str);
138 EXPORT_SYMBOL(cfs_str2mask);
140 /* get the first string out of @str */
141 char *cfs_firststr(char *str, size_t size)
146 /* trim leading spaces */
147 while (i < size && *str && isspace(*str)) {
152 /* string with all spaces */
157 while (i < size && *end != '\0' && !isspace(*end)) {
166 EXPORT_SYMBOL(cfs_firststr);
169 * Extracts tokens from strings.
171 * Looks for \a delim in string \a next, sets \a res to point to
172 * substring before the delimiter, sets \a next right after the found
175 * \retval 1 if \a res points to a string of non-whitespace characters
176 * \retval 0 otherwise
179 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
183 if (next->ls_str == NULL)
186 /* skip leading white spaces */
187 while (next->ls_len) {
188 if (!isspace(*next->ls_str))
194 if (next->ls_len == 0) /* whitespaces only */
197 if (*next->ls_str == delim) {
198 /* first non-writespace is the delimiter */
202 res->ls_str = next->ls_str;
203 end = memchr(next->ls_str, delim, next->ls_len);
205 /* there is no the delimeter in the string */
206 end = next->ls_str + next->ls_len;
209 next->ls_str = end + 1;
210 next->ls_len -= (end - res->ls_str + 1);
213 /* skip ending whitespaces */
214 while (--end != res->ls_str) {
219 res->ls_len = end - res->ls_str + 1;
222 EXPORT_SYMBOL(cfs_gettok);
225 * Converts string to integer.
227 * Accepts decimal and hexadecimal number recordings.
229 * \retval 1 if first \a nob chars of \a str convert to decimal or
230 * hexadecimal integer in the range [\a min, \a max]
231 * \retval 0 otherwise
234 cfs_str2num_check(char *str, int nob, unsigned *num,
235 unsigned min, unsigned max)
237 bool all_numbers = true;
244 * kstrouint can only handle strings composed
245 * of only numbers. We need to scan the string
246 * passed in for the first non-digit character
247 * and end the string at that location. If we
248 * don't find any non-digit character we still
249 * need to place a '\0' at position len since
250 * we are not interested in the rest of the
251 * string which is longer than len in size.
252 * After we are done the character at the
253 * position we placed '\0' must be restored.
255 len = min((int)strlen(endp), nob);
256 for (; endp < str + len; endp++) {
257 if (!isxdigit(*endp) && *endp != '-' &&
264 /* Eat trailing space */
265 if (!all_numbers && isspace(*endp)) {
273 rc = kstrtouint(str, 0, num);
275 if (rc || !all_numbers)
278 return (*num >= min && *num <= max);
280 EXPORT_SYMBOL(cfs_str2num_check);
283 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
284 * \a src should only have a single token which can be \<number\> or \*
286 * \retval pointer to allocated range_expr and initialized
287 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
290 * \<number\> '-' \<number\> |
291 * \<number\> '-' \<number\> '/' \<number\>
292 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
293 * -ENOMEM will be returned.
296 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
297 int bracketed, struct cfs_range_expr **expr)
299 struct cfs_range_expr *re;
302 LIBCFS_ALLOC(re, sizeof(*re));
306 if (src->ls_len == 1 && src->ls_str[0] == '*') {
313 if (cfs_str2num_check(src->ls_str, src->ls_len,
314 &re->re_lo, min, max)) {
315 /* <number> is parsed */
316 re->re_hi = re->re_lo;
321 if (!bracketed || !cfs_gettok(src, '-', &tok))
324 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
325 &re->re_lo, min, max))
329 if (cfs_str2num_check(src->ls_str, src->ls_len,
330 &re->re_hi, min, max)) {
331 /* <number> - <number> is parsed */
336 /* go to check <number> '-' <number> '/' <number> */
337 if (cfs_gettok(src, '/', &tok)) {
338 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
339 &re->re_hi, min, max))
342 /* <number> - <number> / ... */
343 if (cfs_str2num_check(src->ls_str, src->ls_len,
344 &re->re_stride, min, max)) {
345 /* <number> - <number> / <number> is parsed */
355 LIBCFS_FREE(re, sizeof(*re));
360 * Print the range expression \a re into specified \a buffer.
361 * If \a bracketed is true, expression does not need additional
364 * \retval number of characters written
367 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
377 if (expr->re_lo == expr->re_hi)
378 i = scnprintf(buffer, count, "%u", expr->re_lo);
379 else if (expr->re_stride == 1)
380 i = scnprintf(buffer, count, "%s%u-%u%s",
381 s, expr->re_lo, expr->re_hi, e);
383 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
384 s, expr->re_lo, expr->re_hi,
390 * Print a list of range expressions (\a expr_list) into specified \a buffer.
391 * If the list contains several expressions, separate them with comma
392 * and surround the list with brackets.
394 * \retval number of characters written
397 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
399 struct cfs_range_expr *expr;
406 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
410 i += scnprintf(buffer + i, count - i, "[");
412 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
414 i += scnprintf(buffer + i, count - i, ",");
415 i += cfs_range_expr_print(buffer + i, count - i, expr,
420 i += scnprintf(buffer + i, count - i, "]");
424 EXPORT_SYMBOL(cfs_expr_list_print);
427 * Matches value (\a value) against ranges expression list \a expr_list.
429 * \retval 1 if \a value matches
430 * \retval 0 otherwise
433 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
435 struct cfs_range_expr *expr;
437 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
438 if (value >= expr->re_lo && value <= expr->re_hi &&
439 ((value - expr->re_lo) % expr->re_stride) == 0)
445 EXPORT_SYMBOL(cfs_expr_list_match);
448 * Convert express list (\a expr_list) to an array of all matched values
450 * \retval N N is total number of all matched values
451 * \retval 0 if expression list is empty
452 * \retval < 0 for failure
455 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
457 struct cfs_range_expr *expr;
462 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
463 for (i = expr->re_lo; i <= expr->re_hi; i++) {
464 if (((i - expr->re_lo) % expr->re_stride) == 0)
469 if (count == 0) /* empty expression list */
473 CERROR("Number of values %d exceeds max allowed %d\n",
478 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
483 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
484 for (i = expr->re_lo; i <= expr->re_hi; i++) {
485 if (((i - expr->re_lo) % expr->re_stride) == 0)
493 EXPORT_SYMBOL(cfs_expr_list_values);
496 cfs_expr_list_values_free(__u32 *values, int num)
498 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
499 * by OBD_FREE() if it's called by module other than libcfs & LNet,
500 * otherwise we will see fake memory leak */
501 LIBCFS_FREE(values, num * sizeof(values[0]));
503 EXPORT_SYMBOL(cfs_expr_list_values_free);
506 * Frees cfs_range_expr structures of \a expr_list.
511 cfs_expr_list_free(struct cfs_expr_list *expr_list)
513 while (!list_empty(&expr_list->el_exprs)) {
514 struct cfs_range_expr *expr;
516 expr = list_entry(expr_list->el_exprs.next,
517 struct cfs_range_expr, re_link);
518 list_del(&expr->re_link);
519 LIBCFS_FREE(expr, sizeof(*expr));
522 LIBCFS_FREE(expr_list, sizeof(*expr_list));
524 EXPORT_SYMBOL(cfs_expr_list_free);
527 * Parses \<cfs_expr_list\> token of the syntax.
529 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
530 * \retval -errno otherwise
533 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
534 struct cfs_expr_list **elpp)
536 struct cfs_expr_list *expr_list;
537 struct cfs_range_expr *expr;
541 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
542 if (expr_list == NULL)
548 INIT_LIST_HEAD(&expr_list->el_exprs);
550 if (src.ls_str[0] == '[' &&
551 src.ls_str[src.ls_len - 1] == ']') {
556 while (src.ls_str != NULL) {
559 if (!cfs_gettok(&src, ',', &tok)) {
564 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
568 list_add_tail(&expr->re_link,
569 &expr_list->el_exprs);
572 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
574 list_add_tail(&expr->re_link,
575 &expr_list->el_exprs);
580 cfs_expr_list_free(expr_list);
586 EXPORT_SYMBOL(cfs_expr_list_parse);
589 * Frees cfs_expr_list structures of \a list.
591 * For each struct cfs_expr_list structure found on \a list it frees
592 * range_expr list attached to it and frees the cfs_expr_list itself.
597 cfs_expr_list_free_list(struct list_head *list)
599 struct cfs_expr_list *el;
601 while (!list_empty(list)) {
602 el = list_entry(list->next,
603 struct cfs_expr_list, el_link);
604 list_del(&el->el_link);
605 cfs_expr_list_free(el);
608 EXPORT_SYMBOL(cfs_expr_list_free_list);