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 <linux/libcfs/libcfs.h>
45 /* non-0 = don't match */
46 int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
48 if (s1 == NULL || s2 == NULL)
54 while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
55 if (n == 0 || *s1 == '\0' || *s2 == '\0')
61 return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
63 EXPORT_SYMBOL(cfs_strncasecmp);
65 /* Convert a text string to a bitmask */
66 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
67 int *oldmask, int minmask, int allmask)
71 int newmask = minmask, i, len, found = 0;
73 /* <str> must be a list of tokens separated by whitespace
74 * and optionally an operator ('+' or '-'). If an operator
75 * appears first in <str>, '*oldmask' is used as the starting point
76 * (relative), otherwise minmask is used (absolute). An operator
77 * applies to all following tokens up to the next operator. */
83 if (*str == '+' || *str == '-') {
86 /* only if first token is relative */
90 if (*str == 0) /* trailing op */
94 /* find token length */
95 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
96 str[len] != '+' && str[len] != '-'; len++);
100 for (i = 0; i < 32; i++) {
101 debugstr = bit2str(i);
102 if (debugstr != NULL &&
103 strlen(debugstr) == len &&
104 cfs_strncasecmp(str, debugstr, len) == 0) {
106 newmask &= ~(1 << i);
113 if (!found && len == 3 &&
114 (cfs_strncasecmp(str, "ALL", len) == 0)) {
122 CWARN("unknown mask '%.*s'.\n"
123 "mask usage: [+|-]<all|type> ...\n", len, str);
132 EXPORT_SYMBOL(cfs_str2mask);
134 /* get the first string out of @str */
135 char *cfs_firststr(char *str, size_t size)
140 /* trim leading spaces */
141 while (i < size && *str && isspace(*str)) {
146 /* string with all spaces */
151 while (i < size && *end != '\0' && !isspace(*end)) {
160 EXPORT_SYMBOL(cfs_firststr);
163 cfs_trimwhite(char *str)
167 while (cfs_iswhite(*str))
170 end = str + strlen(str);
172 if (!cfs_iswhite(end[-1]))
180 EXPORT_SYMBOL(cfs_trimwhite);
183 * Extracts tokens from strings.
185 * Looks for \a delim in string \a next, sets \a res to point to
186 * substring before the delimiter, sets \a next right after the found
189 * \retval 1 if \a res points to a string of non-whitespace characters
190 * \retval 0 otherwise
193 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
197 if (next->ls_str == NULL)
200 /* skip leading white spaces */
201 while (next->ls_len) {
202 if (!cfs_iswhite(*next->ls_str))
208 if (next->ls_len == 0) /* whitespaces only */
211 if (*next->ls_str == delim) {
212 /* first non-writespace is the delimiter */
216 res->ls_str = next->ls_str;
217 end = memchr(next->ls_str, delim, next->ls_len);
219 /* there is no the delimeter in the string */
220 end = next->ls_str + next->ls_len;
223 next->ls_str = end + 1;
224 next->ls_len -= (end - res->ls_str + 1);
227 /* skip ending whitespaces */
228 while (--end != res->ls_str) {
229 if (!cfs_iswhite(*end))
233 res->ls_len = end - res->ls_str + 1;
236 EXPORT_SYMBOL(cfs_gettok);
239 * Converts string to integer.
241 * Accepts decimal and hexadecimal number recordings.
243 * \retval 1 if first \a nob chars of \a str convert to decimal or
244 * hexadecimal integer in the range [\a min, \a max]
245 * \retval 0 otherwise
248 cfs_str2num_check(char *str, int nob, unsigned *num,
249 unsigned min, unsigned max)
253 str = cfs_trimwhite(str);
254 *num = strtoul(str, &endp, 0);
258 for (; endp < str + nob; endp++) {
259 if (!cfs_iswhite(*endp))
263 return (*num >= min && *num <= max);
265 EXPORT_SYMBOL(cfs_str2num_check);
268 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
269 * \a src should only have a single token which can be \<number\> or \*
271 * \retval pointer to allocated range_expr and initialized
272 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
275 * \<number\> '-' \<number\> |
276 * \<number\> '-' \<number\> '/' \<number\>
277 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
278 * -ENOMEM will be returned.
281 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
282 int bracketed, struct cfs_range_expr **expr)
284 struct cfs_range_expr *re;
287 LIBCFS_ALLOC(re, sizeof(*re));
291 if (src->ls_len == 1 && src->ls_str[0] == '*') {
298 if (cfs_str2num_check(src->ls_str, src->ls_len,
299 &re->re_lo, min, max)) {
300 /* <number> is parsed */
301 re->re_hi = re->re_lo;
306 if (!bracketed || !cfs_gettok(src, '-', &tok))
309 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
310 &re->re_lo, min, max))
314 if (cfs_str2num_check(src->ls_str, src->ls_len,
315 &re->re_hi, min, max)) {
316 /* <number> - <number> is parsed */
321 /* go to check <number> '-' <number> '/' <number> */
322 if (cfs_gettok(src, '/', &tok)) {
323 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
324 &re->re_hi, min, max))
327 /* <number> - <number> / ... */
328 if (cfs_str2num_check(src->ls_str, src->ls_len,
329 &re->re_stride, min, max)) {
330 /* <number> - <number> / <number> is parsed */
340 LIBCFS_FREE(re, sizeof(*re));
343 EXPORT_SYMBOL(cfs_range_expr_parse);
346 * Matches value (\a value) against ranges expression list \a expr_list.
348 * \retval 1 if \a value matches
349 * \retval 0 otherwise
352 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
354 struct cfs_range_expr *expr;
356 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
357 if (value >= expr->re_lo && value <= expr->re_hi &&
358 ((value - expr->re_lo) % expr->re_stride) == 0)
364 EXPORT_SYMBOL(cfs_expr_list_match);
367 * Convert express list (\a expr_list) to an array of all matched values
369 * \retval N N is total number of all matched values
370 * \retval 0 if expression list is empty
371 * \retval < 0 for failure
374 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
376 struct cfs_range_expr *expr;
381 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
382 for (i = expr->re_lo; i <= expr->re_hi; i++) {
383 if (((i - expr->re_lo) % expr->re_stride) == 0)
388 if (count == 0) /* empty expression list */
392 CERROR("Number of values %d exceeds max allowed %d\n",
397 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
402 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
403 for (i = expr->re_lo; i <= expr->re_hi; i++) {
404 if (((i - expr->re_lo) % expr->re_stride) == 0)
412 EXPORT_SYMBOL(cfs_expr_list_values);
415 * Frees cfs_range_expr structures of \a expr_list.
420 cfs_expr_list_free(struct cfs_expr_list *expr_list)
422 while (!list_empty(&expr_list->el_exprs)) {
423 struct cfs_range_expr *expr;
425 expr = list_entry(expr_list->el_exprs.next,
426 struct cfs_range_expr, re_link),
427 list_del(&expr->re_link);
428 LIBCFS_FREE(expr, sizeof(*expr));
431 LIBCFS_FREE(expr_list, sizeof(*expr_list));
433 EXPORT_SYMBOL(cfs_expr_list_free);
436 cfs_expr_list_print(struct cfs_expr_list *expr_list)
438 struct cfs_range_expr *expr;
440 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
441 CDEBUG(D_WARNING, "%d-%d/%d\n",
442 expr->re_lo, expr->re_hi, expr->re_stride);
445 EXPORT_SYMBOL(cfs_expr_list_print);
448 * Parses \<cfs_expr_list\> token of the syntax.
450 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
451 * \retval 0 otherwise
454 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
455 struct cfs_expr_list **elpp)
457 struct cfs_expr_list *expr_list;
458 struct cfs_range_expr *expr;
462 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
463 if (expr_list == NULL)
469 INIT_LIST_HEAD(&expr_list->el_exprs);
471 if (src.ls_str[0] == '[' &&
472 src.ls_str[src.ls_len - 1] == ']') {
477 while (src.ls_str != NULL) {
480 if (!cfs_gettok(&src, ',', &tok)) {
485 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
489 list_add_tail(&expr->re_link,
490 &expr_list->el_exprs);
493 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
495 list_add_tail(&expr->re_link,
496 &expr_list->el_exprs);
501 cfs_expr_list_free(expr_list);
507 EXPORT_SYMBOL(cfs_expr_list_parse);
510 * Frees cfs_expr_list structures of \a list.
512 * For each struct cfs_expr_list structure found on \a list it frees
513 * range_expr list attached to it and frees the cfs_expr_list itself.
518 cfs_expr_list_free_list(struct list_head *list)
520 struct cfs_expr_list *el;
522 while (!list_empty(list)) {
523 el = list_entry(list->next,
524 struct cfs_expr_list, el_link);
525 list_del(&el->el_link);
526 cfs_expr_list_free(el);
529 EXPORT_SYMBOL(cfs_expr_list_free_list);
532 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
534 struct cfs_expr_list *el;
543 while (src.ls_str != NULL) {
546 if (!cfs_gettok(&src, '.', &res)) {
551 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
555 list_add_tail(&el->el_link, list);
564 cfs_expr_list_free_list(list);
568 EXPORT_SYMBOL(cfs_ip_addr_parse);
571 * Matches address (\a addr) against address set encoded in \a list.
573 * \retval 1 if \a addr matches
574 * \retval 0 otherwise
577 cfs_ip_addr_match(__u32 addr, struct list_head *list)
579 struct cfs_expr_list *el;
582 list_for_each_entry_reverse(el, list, el_link) {
583 if (!cfs_expr_list_match(addr & 0xff, el))
591 EXPORT_SYMBOL(cfs_ip_addr_match);
594 cfs_ip_addr_free(struct list_head *list)
596 cfs_expr_list_free_list(list);
598 EXPORT_SYMBOL(cfs_ip_addr_free);