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, 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 /* non-0 = don't match */
73 int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
75 if (s1 == NULL || s2 == NULL)
81 while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
82 if (n == 0 || *s1 == '\0' || *s2 == '\0')
88 return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
90 EXPORT_SYMBOL(cfs_strncasecmp);
92 /* Convert a text string to a bitmask */
93 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
94 int *oldmask, int minmask, int allmask)
98 int newmask = minmask, i, len, found = 0;
101 /* <str> must be a list of tokens separated by whitespace
102 * and optionally an operator ('+' or '-'). If an operator
103 * appears first in <str>, '*oldmask' is used as the starting point
104 * (relative), otherwise minmask is used (absolute). An operator
105 * applies to all following tokens up to the next operator. */
107 while (isspace(*str))
111 if (*str == '+' || *str == '-') {
114 /* only if first token is relative */
116 while (isspace(*str))
118 if (*str == 0) /* trailing op */
122 /* find token length */
123 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
124 str[len] != '+' && str[len] != '-'; len++);
128 for (i = 0; i < 32; i++) {
129 debugstr = bit2str(i);
130 if (debugstr != NULL &&
131 strlen(debugstr) == len &&
132 cfs_strncasecmp(str, debugstr, len) == 0) {
134 newmask &= ~(1 << i);
141 if (!found && len == 3 &&
142 (cfs_strncasecmp(str, "ALL", len) == 0)) {
150 CWARN("unknown mask '%.*s'.\n"
151 "mask usage: [+|-]<all|type> ...\n", len, str);
160 EXPORT_SYMBOL(cfs_str2mask);
162 /* Duplicate a string in a platform-independent way */
163 char *cfs_strdup(const char *str, u_int32_t flags)
165 size_t lenz; /* length of str + zero byte */
168 lenz = strlen(str) + 1;
170 dup_str = kmalloc(lenz, flags);
174 memcpy(dup_str, str, lenz);
178 EXPORT_SYMBOL(cfs_strdup);
181 * cfs_{v}snprintf() return the actual size that is printed rather than
182 * the size that would be printed in standard functions.
185 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
190 i = vsnprintf(buf, size, fmt, args);
192 return (i >= size ? size - 1 : i);
194 EXPORT_SYMBOL(cfs_vsnprintf);
197 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
203 i = cfs_vsnprintf(buf, size, fmt, args);
208 EXPORT_SYMBOL(cfs_snprintf);
210 /* get the first string out of @str */
211 char *cfs_firststr(char *str, size_t size)
216 /* trim leading spaces */
217 while (i < size && *str && isspace(*str)) {
222 /* string with all spaces */
227 while (i < size && *end != '\0' && !isspace(*end)) {
236 EXPORT_SYMBOL(cfs_firststr);
239 cfs_trimwhite(char *str)
243 while (cfs_iswhite(*str))
246 end = str + strlen(str);
248 if (!cfs_iswhite(end[-1]))
256 EXPORT_SYMBOL(cfs_trimwhite);
259 * Extracts tokens from strings.
261 * Looks for \a delim in string \a next, sets \a res to point to
262 * substring before the delimiter, sets \a next right after the found
265 * \retval 1 if \a res points to a string of non-whitespace characters
266 * \retval 0 otherwise
269 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
273 if (next->ls_str == NULL)
276 /* skip leading white spaces */
277 while (next->ls_len) {
278 if (!cfs_iswhite(*next->ls_str))
284 if (next->ls_len == 0) /* whitespaces only */
287 if (*next->ls_str == delim) {
288 /* first non-writespace is the delimiter */
292 res->ls_str = next->ls_str;
293 end = memchr(next->ls_str, delim, next->ls_len);
295 /* there is no the delimeter in the string */
296 end = next->ls_str + next->ls_len;
299 next->ls_str = end + 1;
300 next->ls_len -= (end - res->ls_str + 1);
303 /* skip ending whitespaces */
304 while (--end != res->ls_str) {
305 if (!cfs_iswhite(*end))
309 res->ls_len = end - res->ls_str + 1;
312 EXPORT_SYMBOL(cfs_gettok);
315 * Converts string to integer.
317 * Accepts decimal and hexadecimal number recordings.
319 * \retval 1 if first \a nob chars of \a str convert to decimal or
320 * hexadecimal integer in the range [\a min, \a max]
321 * \retval 0 otherwise
324 cfs_str2num_check(char *str, int nob, unsigned *num,
325 unsigned min, unsigned max)
329 *num = strtoul(str, &endp, 0);
333 for (; endp < str + nob; endp++) {
334 if (!cfs_iswhite(*endp))
338 return (*num >= min && *num <= max);
340 EXPORT_SYMBOL(cfs_str2num_check);
343 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
344 * \a src should only have a single token which can be \<number\> or \*
346 * \retval pointer to allocated range_expr and initialized
347 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
350 * \<number\> '-' \<number\> |
351 * \<number\> '-' \<number\> '/' \<number\>
352 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
353 * -ENOMEM will be returned.
356 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
357 int bracketed, struct cfs_range_expr **expr)
359 struct cfs_range_expr *re;
362 LIBCFS_ALLOC(re, sizeof(*re));
366 if (src->ls_len == 1 && src->ls_str[0] == '*') {
373 if (cfs_str2num_check(src->ls_str, src->ls_len,
374 &re->re_lo, min, max)) {
375 /* <number> is parsed */
376 re->re_hi = re->re_lo;
381 if (!bracketed || !cfs_gettok(src, '-', &tok))
384 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
385 &re->re_lo, min, max))
389 if (cfs_str2num_check(src->ls_str, src->ls_len,
390 &re->re_hi, min, max)) {
391 /* <number> - <number> is parsed */
396 /* go to check <number> '-' <number> '/' <number> */
397 if (cfs_gettok(src, '/', &tok)) {
398 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
399 &re->re_hi, min, max))
402 /* <number> - <number> / ... */
403 if (cfs_str2num_check(src->ls_str, src->ls_len,
404 &re->re_stride, min, max)) {
405 /* <number> - <number> / <number> is parsed */
415 LIBCFS_FREE(re, sizeof(*re));
418 EXPORT_SYMBOL(cfs_range_expr_parse);
421 * Print the range expression \a re into specified \a buffer.
422 * If \a bracketed is true, expression does not need additional
425 * \retval number of characters written
428 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
438 if (expr->re_lo == expr->re_hi)
439 i = cfs_snprintf(buffer, count, "%u", expr->re_lo);
440 else if (expr->re_stride == 1)
441 i = cfs_snprintf(buffer, count, "%s%u-%u%s",
442 s, expr->re_lo, expr->re_hi, e);
444 i = cfs_snprintf(buffer, count, "%s%u-%u/%u%s",
445 s, expr->re_lo, expr->re_hi,
451 * Print a list of range expressions (\a expr_list) into specified \a buffer.
452 * If the list contains several expressions, separate them with comma
453 * and surround the list with brackets.
455 * \retval number of characters written
458 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
460 struct cfs_range_expr *expr;
467 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
471 i += cfs_snprintf(buffer + i, count - i, "[");
473 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
475 i += cfs_snprintf(buffer + i, count - i, ",");
476 i += cfs_range_expr_print(buffer + i, count - i, expr,
481 i += cfs_snprintf(buffer + i, count - i, "]");
485 EXPORT_SYMBOL(cfs_expr_list_print);
488 * Matches value (\a value) against ranges expression list \a expr_list.
490 * \retval 1 if \a value matches
491 * \retval 0 otherwise
494 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
496 struct cfs_range_expr *expr;
498 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
499 if (value >= expr->re_lo && value <= expr->re_hi &&
500 ((value - expr->re_lo) % expr->re_stride) == 0)
506 EXPORT_SYMBOL(cfs_expr_list_match);
509 * Convert express list (\a expr_list) to an array of all matched values
511 * \retval N N is total number of all matched values
512 * \retval 0 if expression list is empty
513 * \retval < 0 for failure
516 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
518 struct cfs_range_expr *expr;
523 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
524 for (i = expr->re_lo; i <= expr->re_hi; i++) {
525 if (((i - expr->re_lo) % expr->re_stride) == 0)
530 if (count == 0) /* empty expression list */
534 CERROR("Number of values %d exceeds max allowed %d\n",
539 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
544 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
545 for (i = expr->re_lo; i <= expr->re_hi; i++) {
546 if (((i - expr->re_lo) % expr->re_stride) == 0)
554 EXPORT_SYMBOL(cfs_expr_list_values);
557 * Frees cfs_range_expr structures of \a expr_list.
562 cfs_expr_list_free(struct cfs_expr_list *expr_list)
564 while (!list_empty(&expr_list->el_exprs)) {
565 struct cfs_range_expr *expr;
567 expr = list_entry(expr_list->el_exprs.next,
568 struct cfs_range_expr, re_link),
569 list_del(&expr->re_link);
570 LIBCFS_FREE(expr, sizeof(*expr));
573 LIBCFS_FREE(expr_list, sizeof(*expr_list));
575 EXPORT_SYMBOL(cfs_expr_list_free);
578 * Parses \<cfs_expr_list\> token of the syntax.
580 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
581 * \retval -errno otherwise
584 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
585 struct cfs_expr_list **elpp)
587 struct cfs_expr_list *expr_list;
588 struct cfs_range_expr *expr;
592 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
593 if (expr_list == NULL)
599 INIT_LIST_HEAD(&expr_list->el_exprs);
601 if (src.ls_str[0] == '[' &&
602 src.ls_str[src.ls_len - 1] == ']') {
607 while (src.ls_str != NULL) {
610 if (!cfs_gettok(&src, ',', &tok)) {
615 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
619 list_add_tail(&expr->re_link,
620 &expr_list->el_exprs);
623 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
625 list_add_tail(&expr->re_link,
626 &expr_list->el_exprs);
631 cfs_expr_list_free(expr_list);
637 EXPORT_SYMBOL(cfs_expr_list_parse);
640 * Frees cfs_expr_list structures of \a list.
642 * For each struct cfs_expr_list structure found on \a list it frees
643 * range_expr list attached to it and frees the cfs_expr_list itself.
648 cfs_expr_list_free_list(struct list_head *list)
650 struct cfs_expr_list *el;
652 while (!list_empty(list)) {
653 el = list_entry(list->next,
654 struct cfs_expr_list, el_link);
655 list_del(&el->el_link);
656 cfs_expr_list_free(el);
659 EXPORT_SYMBOL(cfs_expr_list_free_list);
662 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
664 struct cfs_expr_list *el;
673 while (src.ls_str != NULL) {
676 if (!cfs_gettok(&src, '.', &res)) {
681 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
685 list_add_tail(&el->el_link, list);
694 cfs_expr_list_free_list(list);
698 EXPORT_SYMBOL(cfs_ip_addr_parse);
701 * Matches address (\a addr) against address set encoded in \a list.
703 * \retval 1 if \a addr matches
704 * \retval 0 otherwise
707 cfs_ip_addr_match(__u32 addr, struct list_head *list)
709 struct cfs_expr_list *el;
712 list_for_each_entry_reverse(el, list, el_link) {
713 if (!cfs_expr_list_match(addr & 0xff, el))
721 EXPORT_SYMBOL(cfs_ip_addr_match);
724 cfs_ip_addr_free(struct list_head *list)
726 cfs_expr_list_free_list(list);
728 EXPORT_SYMBOL(cfs_ip_addr_free);