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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * String manipulation functions.
38 * libcfs/libcfs/libcfs_string.c
40 * Author: Nathan Rutman <nathan.rutman@sun.com>
43 #include <libcfs/libcfs.h>
45 char *cfs_strrstr(const char *haystack, const char *needle)
49 if (unlikely(haystack == NULL || needle == NULL))
52 if (strlen(needle) == 1)
53 return strrchr(haystack, needle[0]);
55 ptr = strstr(haystack, needle);
60 tmp = strstr(&ptr[1], needle);
70 EXPORT_SYMBOL(cfs_strrstr);
72 /* Convert a text string to a bitmask */
73 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
74 int *oldmask, int minmask, int allmask)
78 int newmask = minmask, i, len, found = 0;
81 /* <str> must be a list of tokens separated by whitespace
82 * and optionally an operator ('+' or '-'). If an operator
83 * appears first in <str>, '*oldmask' is used as the starting point
84 * (relative), otherwise minmask is used (absolute). An operator
85 * applies to all following tokens up to the next operator. */
91 if (*str == '+' || *str == '-') {
94 /* only if first token is relative */
98 if (*str == 0) /* trailing op */
102 /* find token length */
103 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
104 str[len] != '+' && str[len] != '-'; len++);
108 for (i = 0; i < 32; i++) {
109 debugstr = bit2str(i);
110 if (debugstr != NULL &&
111 strlen(debugstr) == len &&
112 strncasecmp(str, debugstr, len) == 0) {
114 newmask &= ~(1 << i);
121 if (!found && len == 3 &&
122 (strncasecmp(str, "ALL", len) == 0)) {
130 CWARN("unknown mask '%.*s'.\n"
131 "mask usage: [+|-]<all|type> ...\n", len, str);
140 EXPORT_SYMBOL(cfs_str2mask);
143 * cfs_{v}snprintf() return the actual size that is printed rather than
144 * the size that would be printed in standard functions.
147 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
152 i = vsnprintf(buf, size, fmt, args);
154 return (i >= size ? size - 1 : i);
156 EXPORT_SYMBOL(cfs_vsnprintf);
159 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
165 i = cfs_vsnprintf(buf, size, fmt, args);
170 EXPORT_SYMBOL(cfs_snprintf);
172 /* get the first string out of @str */
173 char *cfs_firststr(char *str, size_t size)
178 /* trim leading spaces */
179 while (i < size && *str && isspace(*str)) {
184 /* string with all spaces */
189 while (i < size && *end != '\0' && !isspace(*end)) {
198 EXPORT_SYMBOL(cfs_firststr);
201 cfs_trimwhite(char *str)
205 while (isspace(*str))
208 end = str + strlen(str);
210 if (!isspace(end[-1]))
218 EXPORT_SYMBOL(cfs_trimwhite);
221 * Extracts tokens from strings.
223 * Looks for \a delim in string \a next, sets \a res to point to
224 * substring before the delimiter, sets \a next right after the found
227 * \retval 1 if \a res points to a string of non-whitespace characters
228 * \retval 0 otherwise
231 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
235 if (next->ls_str == NULL)
238 /* skip leading white spaces */
239 while (next->ls_len) {
240 if (!isspace(*next->ls_str))
246 if (next->ls_len == 0) /* whitespaces only */
249 if (*next->ls_str == delim) {
250 /* first non-writespace is the delimiter */
254 res->ls_str = next->ls_str;
255 end = memchr(next->ls_str, delim, next->ls_len);
257 /* there is no the delimeter in the string */
258 end = next->ls_str + next->ls_len;
261 next->ls_str = end + 1;
262 next->ls_len -= (end - res->ls_str + 1);
265 /* skip ending whitespaces */
266 while (--end != res->ls_str) {
271 res->ls_len = end - res->ls_str + 1;
274 EXPORT_SYMBOL(cfs_gettok);
277 * Converts string to integer.
279 * Accepts decimal and hexadecimal number recordings.
281 * \retval 1 if first \a nob chars of \a str convert to decimal or
282 * hexadecimal integer in the range [\a min, \a max]
283 * \retval 0 otherwise
286 cfs_str2num_check(char *str, int nob, unsigned *num,
287 unsigned min, unsigned max)
291 *num = simple_strtoul(str, &endp, 0);
295 for (; endp < str + nob; endp++) {
300 return (*num >= min && *num <= max);
302 EXPORT_SYMBOL(cfs_str2num_check);
305 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
306 * \a src should only have a single token which can be \<number\> or \*
308 * \retval pointer to allocated range_expr and initialized
309 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
312 * \<number\> '-' \<number\> |
313 * \<number\> '-' \<number\> '/' \<number\>
314 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
315 * -ENOMEM will be returned.
318 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
319 int bracketed, struct cfs_range_expr **expr)
321 struct cfs_range_expr *re;
324 LIBCFS_ALLOC(re, sizeof(*re));
328 if (src->ls_len == 1 && src->ls_str[0] == '*') {
335 if (cfs_str2num_check(src->ls_str, src->ls_len,
336 &re->re_lo, min, max)) {
337 /* <number> is parsed */
338 re->re_hi = re->re_lo;
343 if (!bracketed || !cfs_gettok(src, '-', &tok))
346 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
347 &re->re_lo, min, max))
351 if (cfs_str2num_check(src->ls_str, src->ls_len,
352 &re->re_hi, min, max)) {
353 /* <number> - <number> is parsed */
358 /* go to check <number> '-' <number> '/' <number> */
359 if (cfs_gettok(src, '/', &tok)) {
360 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
361 &re->re_hi, min, max))
364 /* <number> - <number> / ... */
365 if (cfs_str2num_check(src->ls_str, src->ls_len,
366 &re->re_stride, min, max)) {
367 /* <number> - <number> / <number> is parsed */
377 LIBCFS_FREE(re, sizeof(*re));
380 EXPORT_SYMBOL(cfs_range_expr_parse);
383 * Print the range expression \a re into specified \a buffer.
384 * If \a bracketed is true, expression does not need additional
387 * \retval number of characters written
390 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
400 if (expr->re_lo == expr->re_hi)
401 i = cfs_snprintf(buffer, count, "%u", expr->re_lo);
402 else if (expr->re_stride == 1)
403 i = cfs_snprintf(buffer, count, "%s%u-%u%s",
404 s, expr->re_lo, expr->re_hi, e);
406 i = cfs_snprintf(buffer, count, "%s%u-%u/%u%s",
407 s, expr->re_lo, expr->re_hi,
413 * Print a list of range expressions (\a expr_list) into specified \a buffer.
414 * If the list contains several expressions, separate them with comma
415 * and surround the list with brackets.
417 * \retval number of characters written
420 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
422 struct cfs_range_expr *expr;
429 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
433 i += cfs_snprintf(buffer + i, count - i, "[");
435 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
437 i += cfs_snprintf(buffer + i, count - i, ",");
438 i += cfs_range_expr_print(buffer + i, count - i, expr,
443 i += cfs_snprintf(buffer + i, count - i, "]");
447 EXPORT_SYMBOL(cfs_expr_list_print);
450 * Matches value (\a value) against ranges expression list \a expr_list.
452 * \retval 1 if \a value matches
453 * \retval 0 otherwise
456 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
458 struct cfs_range_expr *expr;
460 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
461 if (value >= expr->re_lo && value <= expr->re_hi &&
462 ((value - expr->re_lo) % expr->re_stride) == 0)
468 EXPORT_SYMBOL(cfs_expr_list_match);
471 * Convert express list (\a expr_list) to an array of all matched values
473 * \retval N N is total number of all matched values
474 * \retval 0 if expression list is empty
475 * \retval < 0 for failure
478 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
480 struct cfs_range_expr *expr;
485 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
486 for (i = expr->re_lo; i <= expr->re_hi; i++) {
487 if (((i - expr->re_lo) % expr->re_stride) == 0)
492 if (count == 0) /* empty expression list */
496 CERROR("Number of values %d exceeds max allowed %d\n",
501 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
506 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
507 for (i = expr->re_lo; i <= expr->re_hi; i++) {
508 if (((i - expr->re_lo) % expr->re_stride) == 0)
516 EXPORT_SYMBOL(cfs_expr_list_values);
519 * Frees cfs_range_expr structures of \a expr_list.
524 cfs_expr_list_free(struct cfs_expr_list *expr_list)
526 while (!list_empty(&expr_list->el_exprs)) {
527 struct cfs_range_expr *expr;
529 expr = list_entry(expr_list->el_exprs.next,
530 struct cfs_range_expr, re_link);
531 list_del(&expr->re_link);
532 LIBCFS_FREE(expr, sizeof(*expr));
535 LIBCFS_FREE(expr_list, sizeof(*expr_list));
537 EXPORT_SYMBOL(cfs_expr_list_free);
540 * Parses \<cfs_expr_list\> token of the syntax.
542 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
543 * \retval -errno otherwise
546 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
547 struct cfs_expr_list **elpp)
549 struct cfs_expr_list *expr_list;
550 struct cfs_range_expr *expr;
554 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
555 if (expr_list == NULL)
561 INIT_LIST_HEAD(&expr_list->el_exprs);
563 if (src.ls_str[0] == '[' &&
564 src.ls_str[src.ls_len - 1] == ']') {
569 while (src.ls_str != NULL) {
572 if (!cfs_gettok(&src, ',', &tok)) {
577 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
581 list_add_tail(&expr->re_link,
582 &expr_list->el_exprs);
585 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
587 list_add_tail(&expr->re_link,
588 &expr_list->el_exprs);
593 cfs_expr_list_free(expr_list);
599 EXPORT_SYMBOL(cfs_expr_list_parse);
602 * Frees cfs_expr_list structures of \a list.
604 * For each struct cfs_expr_list structure found on \a list it frees
605 * range_expr list attached to it and frees the cfs_expr_list itself.
610 cfs_expr_list_free_list(struct list_head *list)
612 struct cfs_expr_list *el;
614 while (!list_empty(list)) {
615 el = list_entry(list->next,
616 struct cfs_expr_list, el_link);
617 list_del(&el->el_link);
618 cfs_expr_list_free(el);
621 EXPORT_SYMBOL(cfs_expr_list_free_list);
624 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
626 struct cfs_expr_list *el;
635 while (src.ls_str != NULL) {
638 if (!cfs_gettok(&src, '.', &res)) {
643 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
647 list_add_tail(&el->el_link, list);
656 cfs_expr_list_free_list(list);
660 EXPORT_SYMBOL(cfs_ip_addr_parse);
663 * Matches address (\a addr) against address set encoded in \a list.
665 * \retval 1 if \a addr matches
666 * \retval 0 otherwise
669 cfs_ip_addr_match(__u32 addr, struct list_head *list)
671 struct cfs_expr_list *el;
674 list_for_each_entry_reverse(el, list, el_link) {
675 if (!cfs_expr_list_match(addr & 0xff, el))
683 EXPORT_SYMBOL(cfs_ip_addr_match);