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);
142 /* Duplicate a string in a platform-independent way */
143 char *cfs_strdup(const char *str, u_int32_t flags)
145 size_t lenz; /* length of str + zero byte */
148 lenz = strlen(str) + 1;
150 dup_str = kmalloc(lenz, flags);
154 memcpy(dup_str, str, lenz);
158 EXPORT_SYMBOL(cfs_strdup);
161 * cfs_{v}snprintf() return the actual size that is printed rather than
162 * the size that would be printed in standard functions.
165 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
170 i = vsnprintf(buf, size, fmt, args);
172 return (i >= size ? size - 1 : i);
174 EXPORT_SYMBOL(cfs_vsnprintf);
177 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
183 i = cfs_vsnprintf(buf, size, fmt, args);
188 EXPORT_SYMBOL(cfs_snprintf);
190 /* get the first string out of @str */
191 char *cfs_firststr(char *str, size_t size)
196 /* trim leading spaces */
197 while (i < size && *str && isspace(*str)) {
202 /* string with all spaces */
207 while (i < size && *end != '\0' && !isspace(*end)) {
216 EXPORT_SYMBOL(cfs_firststr);
219 cfs_trimwhite(char *str)
223 while (cfs_iswhite(*str))
226 end = str + strlen(str);
228 if (!cfs_iswhite(end[-1]))
236 EXPORT_SYMBOL(cfs_trimwhite);
239 * Extracts tokens from strings.
241 * Looks for \a delim in string \a next, sets \a res to point to
242 * substring before the delimiter, sets \a next right after the found
245 * \retval 1 if \a res points to a string of non-whitespace characters
246 * \retval 0 otherwise
249 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
253 if (next->ls_str == NULL)
256 /* skip leading white spaces */
257 while (next->ls_len) {
258 if (!cfs_iswhite(*next->ls_str))
264 if (next->ls_len == 0) /* whitespaces only */
267 if (*next->ls_str == delim) {
268 /* first non-writespace is the delimiter */
272 res->ls_str = next->ls_str;
273 end = memchr(next->ls_str, delim, next->ls_len);
275 /* there is no the delimeter in the string */
276 end = next->ls_str + next->ls_len;
279 next->ls_str = end + 1;
280 next->ls_len -= (end - res->ls_str + 1);
283 /* skip ending whitespaces */
284 while (--end != res->ls_str) {
285 if (!cfs_iswhite(*end))
289 res->ls_len = end - res->ls_str + 1;
292 EXPORT_SYMBOL(cfs_gettok);
295 * Converts string to integer.
297 * Accepts decimal and hexadecimal number recordings.
299 * \retval 1 if first \a nob chars of \a str convert to decimal or
300 * hexadecimal integer in the range [\a min, \a max]
301 * \retval 0 otherwise
304 cfs_str2num_check(char *str, int nob, unsigned *num,
305 unsigned min, unsigned max)
309 *num = strtoul(str, &endp, 0);
313 for (; endp < str + nob; endp++) {
314 if (!cfs_iswhite(*endp))
318 return (*num >= min && *num <= max);
320 EXPORT_SYMBOL(cfs_str2num_check);
323 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
324 * \a src should only have a single token which can be \<number\> or \*
326 * \retval pointer to allocated range_expr and initialized
327 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
330 * \<number\> '-' \<number\> |
331 * \<number\> '-' \<number\> '/' \<number\>
332 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
333 * -ENOMEM will be returned.
336 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
337 int bracketed, struct cfs_range_expr **expr)
339 struct cfs_range_expr *re;
342 LIBCFS_ALLOC(re, sizeof(*re));
346 if (src->ls_len == 1 && src->ls_str[0] == '*') {
353 if (cfs_str2num_check(src->ls_str, src->ls_len,
354 &re->re_lo, min, max)) {
355 /* <number> is parsed */
356 re->re_hi = re->re_lo;
361 if (!bracketed || !cfs_gettok(src, '-', &tok))
364 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
365 &re->re_lo, min, max))
369 if (cfs_str2num_check(src->ls_str, src->ls_len,
370 &re->re_hi, min, max)) {
371 /* <number> - <number> is parsed */
376 /* go to check <number> '-' <number> '/' <number> */
377 if (cfs_gettok(src, '/', &tok)) {
378 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
379 &re->re_hi, min, max))
382 /* <number> - <number> / ... */
383 if (cfs_str2num_check(src->ls_str, src->ls_len,
384 &re->re_stride, min, max)) {
385 /* <number> - <number> / <number> is parsed */
395 LIBCFS_FREE(re, sizeof(*re));
398 EXPORT_SYMBOL(cfs_range_expr_parse);
401 * Print the range expression \a re into specified \a buffer.
402 * If \a bracketed is true, expression does not need additional
405 * \retval number of characters written
408 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
418 if (expr->re_lo == expr->re_hi)
419 i = cfs_snprintf(buffer, count, "%u", expr->re_lo);
420 else if (expr->re_stride == 1)
421 i = cfs_snprintf(buffer, count, "%s%u-%u%s",
422 s, expr->re_lo, expr->re_hi, e);
424 i = cfs_snprintf(buffer, count, "%s%u-%u/%u%s",
425 s, expr->re_lo, expr->re_hi,
431 * Print a list of range expressions (\a expr_list) into specified \a buffer.
432 * If the list contains several expressions, separate them with comma
433 * and surround the list with brackets.
435 * \retval number of characters written
438 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
440 struct cfs_range_expr *expr;
447 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
451 i += cfs_snprintf(buffer + i, count - i, "[");
453 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
455 i += cfs_snprintf(buffer + i, count - i, ",");
456 i += cfs_range_expr_print(buffer + i, count - i, expr,
461 i += cfs_snprintf(buffer + i, count - i, "]");
465 EXPORT_SYMBOL(cfs_expr_list_print);
468 * Matches value (\a value) against ranges expression list \a expr_list.
470 * \retval 1 if \a value matches
471 * \retval 0 otherwise
474 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
476 struct cfs_range_expr *expr;
478 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
479 if (value >= expr->re_lo && value <= expr->re_hi &&
480 ((value - expr->re_lo) % expr->re_stride) == 0)
486 EXPORT_SYMBOL(cfs_expr_list_match);
489 * Convert express list (\a expr_list) to an array of all matched values
491 * \retval N N is total number of all matched values
492 * \retval 0 if expression list is empty
493 * \retval < 0 for failure
496 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
498 struct cfs_range_expr *expr;
503 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
504 for (i = expr->re_lo; i <= expr->re_hi; i++) {
505 if (((i - expr->re_lo) % expr->re_stride) == 0)
510 if (count == 0) /* empty expression list */
514 CERROR("Number of values %d exceeds max allowed %d\n",
519 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
524 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
525 for (i = expr->re_lo; i <= expr->re_hi; i++) {
526 if (((i - expr->re_lo) % expr->re_stride) == 0)
534 EXPORT_SYMBOL(cfs_expr_list_values);
537 * Frees cfs_range_expr structures of \a expr_list.
542 cfs_expr_list_free(struct cfs_expr_list *expr_list)
544 while (!list_empty(&expr_list->el_exprs)) {
545 struct cfs_range_expr *expr;
547 expr = list_entry(expr_list->el_exprs.next,
548 struct cfs_range_expr, re_link),
549 list_del(&expr->re_link);
550 LIBCFS_FREE(expr, sizeof(*expr));
553 LIBCFS_FREE(expr_list, sizeof(*expr_list));
555 EXPORT_SYMBOL(cfs_expr_list_free);
558 * Parses \<cfs_expr_list\> token of the syntax.
560 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
561 * \retval -errno otherwise
564 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
565 struct cfs_expr_list **elpp)
567 struct cfs_expr_list *expr_list;
568 struct cfs_range_expr *expr;
572 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
573 if (expr_list == NULL)
579 INIT_LIST_HEAD(&expr_list->el_exprs);
581 if (src.ls_str[0] == '[' &&
582 src.ls_str[src.ls_len - 1] == ']') {
587 while (src.ls_str != NULL) {
590 if (!cfs_gettok(&src, ',', &tok)) {
595 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
599 list_add_tail(&expr->re_link,
600 &expr_list->el_exprs);
603 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
605 list_add_tail(&expr->re_link,
606 &expr_list->el_exprs);
611 cfs_expr_list_free(expr_list);
617 EXPORT_SYMBOL(cfs_expr_list_parse);
620 * Frees cfs_expr_list structures of \a list.
622 * For each struct cfs_expr_list structure found on \a list it frees
623 * range_expr list attached to it and frees the cfs_expr_list itself.
628 cfs_expr_list_free_list(struct list_head *list)
630 struct cfs_expr_list *el;
632 while (!list_empty(list)) {
633 el = list_entry(list->next,
634 struct cfs_expr_list, el_link);
635 list_del(&el->el_link);
636 cfs_expr_list_free(el);
639 EXPORT_SYMBOL(cfs_expr_list_free_list);
642 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
644 struct cfs_expr_list *el;
653 while (src.ls_str != NULL) {
656 if (!cfs_gettok(&src, '.', &res)) {
661 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
665 list_add_tail(&el->el_link, list);
674 cfs_expr_list_free_list(list);
678 EXPORT_SYMBOL(cfs_ip_addr_parse);
681 * Matches address (\a addr) against address set encoded in \a list.
683 * \retval 1 if \a addr matches
684 * \retval 0 otherwise
687 cfs_ip_addr_match(__u32 addr, struct list_head *list)
689 struct cfs_expr_list *el;
692 list_for_each_entry_reverse(el, list, el_link) {
693 if (!cfs_expr_list_match(addr & 0xff, el))
701 EXPORT_SYMBOL(cfs_ip_addr_match);
704 cfs_ip_addr_free(struct list_head *list)
706 cfs_expr_list_free_list(list);
708 EXPORT_SYMBOL(cfs_ip_addr_free);