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 "../../include/linux/libcfs/libcfs.h"
45 /* Convert a text string to a bitmask */
46 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
47 int *oldmask, int minmask, int allmask)
51 int newmask = minmask, i, len, found = 0;
53 /* <str> must be a list of tokens separated by whitespace
54 * and optionally an operator ('+' or '-'). If an operator
55 * appears first in <str>, '*oldmask' is used as the starting point
56 * (relative), otherwise minmask is used (absolute). An operator
57 * applies to all following tokens up to the next operator. */
63 if (*str == '+' || *str == '-') {
66 /* only if first token is relative */
70 if (*str == 0) /* trailing op */
74 /* find token length */
75 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
76 str[len] != '+' && str[len] != '-'; len++);
80 for (i = 0; i < 32; i++) {
81 debugstr = bit2str(i);
82 if (debugstr != NULL &&
83 strlen(debugstr) == len &&
84 strncasecmp(str, debugstr, len) == 0) {
93 if (!found && len == 3 &&
94 (strncasecmp(str, "ALL", len) == 0)) {
102 CWARN("unknown mask '%.*s'.\n"
103 "mask usage: [+|-]<all|type> ...\n", len, str);
113 /* get the first string out of @str */
114 char *cfs_firststr(char *str, size_t size)
119 /* trim leading spaces */
120 while (i < size && *str && isspace(*str)) {
125 /* string with all spaces */
130 while (i < size && *end != '\0' && !isspace(*end)) {
139 EXPORT_SYMBOL(cfs_firststr);
142 cfs_trimwhite(char *str)
146 while (isspace(*str))
149 end = str + strlen(str);
151 if (!isspace(end[-1]))
159 EXPORT_SYMBOL(cfs_trimwhite);
162 * Extracts tokens from strings.
164 * Looks for \a delim in string \a next, sets \a res to point to
165 * substring before the delimiter, sets \a next right after the found
168 * \retval 1 if \a res points to a string of non-whitespace characters
169 * \retval 0 otherwise
172 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
176 if (next->ls_str == NULL)
179 /* skip leading white spaces */
180 while (next->ls_len) {
181 if (!isspace(*next->ls_str))
187 if (next->ls_len == 0) /* whitespaces only */
190 if (*next->ls_str == delim) {
191 /* first non-writespace is the delimiter */
195 res->ls_str = next->ls_str;
196 end = memchr(next->ls_str, delim, next->ls_len);
198 /* there is no the delimeter in the string */
199 end = next->ls_str + next->ls_len;
202 next->ls_str = end + 1;
203 next->ls_len -= (end - res->ls_str + 1);
206 /* skip ending whitespaces */
207 while (--end != res->ls_str) {
212 res->ls_len = end - res->ls_str + 1;
217 * Converts string to integer.
219 * Accepts decimal and hexadecimal number recordings.
221 * \retval 1 if first \a nob chars of \a str convert to decimal or
222 * hexadecimal integer in the range [\a min, \a max]
223 * \retval 0 otherwise
226 cfs_str2num_check(char *str, int nob, unsigned *num,
227 unsigned min, unsigned max)
231 str = cfs_trimwhite(str);
232 *num = strtoul(str, &endp, 0);
236 for (; endp < str + nob; endp++) {
241 return (*num >= min && *num <= max);
245 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
246 * \a src should only have a single token which can be \<number\> or \*
248 * \retval pointer to allocated range_expr and initialized
249 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
252 * \<number\> '-' \<number\> |
253 * \<number\> '-' \<number\> '/' \<number\>
254 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
255 * -ENOMEM will be returned.
258 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
259 int bracketed, struct cfs_range_expr **expr)
261 struct cfs_range_expr *re;
264 LIBCFS_ALLOC(re, sizeof(*re));
268 if (src->ls_len == 1 && src->ls_str[0] == '*') {
275 if (cfs_str2num_check(src->ls_str, src->ls_len,
276 &re->re_lo, min, max)) {
277 /* <number> is parsed */
278 re->re_hi = re->re_lo;
283 if (!bracketed || !cfs_gettok(src, '-', &tok))
286 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
287 &re->re_lo, min, max))
291 if (cfs_str2num_check(src->ls_str, src->ls_len,
292 &re->re_hi, min, max)) {
293 /* <number> - <number> is parsed */
298 /* go to check <number> '-' <number> '/' <number> */
299 if (cfs_gettok(src, '/', &tok)) {
300 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
301 &re->re_hi, min, max))
304 /* <number> - <number> / ... */
305 if (cfs_str2num_check(src->ls_str, src->ls_len,
306 &re->re_stride, min, max)) {
307 /* <number> - <number> / <number> is parsed */
317 LIBCFS_FREE(re, sizeof(*re));
322 * Matches value (\a value) against ranges expression list \a expr_list.
324 * \retval 1 if \a value matches
325 * \retval 0 otherwise
328 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
330 struct cfs_range_expr *expr;
332 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
333 if (value >= expr->re_lo && value <= expr->re_hi &&
334 ((value - expr->re_lo) % expr->re_stride) == 0)
342 * Convert express list (\a expr_list) to an array of all matched values
344 * \retval N N is total number of all matched values
345 * \retval 0 if expression list is empty
346 * \retval < 0 for failure
349 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
351 struct cfs_range_expr *expr;
356 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
357 for (i = expr->re_lo; i <= expr->re_hi; i++) {
358 if (((i - expr->re_lo) % expr->re_stride) == 0)
363 if (count == 0) /* empty expression list */
367 CERROR("Number of values %d exceeds max allowed %d\n",
372 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
377 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
378 for (i = expr->re_lo; i <= expr->re_hi; i++) {
379 if (((i - expr->re_lo) % expr->re_stride) == 0)
387 EXPORT_SYMBOL(cfs_expr_list_values);
390 * Frees cfs_range_expr structures of \a expr_list.
395 cfs_expr_list_free(struct cfs_expr_list *expr_list)
397 while (!list_empty(&expr_list->el_exprs)) {
398 struct cfs_range_expr *expr;
400 expr = list_entry(expr_list->el_exprs.next,
401 struct cfs_range_expr, re_link),
402 list_del(&expr->re_link);
403 LIBCFS_FREE(expr, sizeof(*expr));
406 LIBCFS_FREE(expr_list, sizeof(*expr_list));
408 EXPORT_SYMBOL(cfs_expr_list_free);
411 * Parses \<cfs_expr_list\> token of the syntax.
413 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
414 * \retval 0 otherwise
417 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
418 struct cfs_expr_list **elpp)
420 struct cfs_expr_list *expr_list;
421 struct cfs_range_expr *expr;
425 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
426 if (expr_list == NULL)
432 INIT_LIST_HEAD(&expr_list->el_exprs);
434 if (src.ls_str[0] == '[' &&
435 src.ls_str[src.ls_len - 1] == ']') {
440 while (src.ls_str != NULL) {
443 if (!cfs_gettok(&src, ',', &tok)) {
448 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
452 list_add_tail(&expr->re_link,
453 &expr_list->el_exprs);
456 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
458 list_add_tail(&expr->re_link,
459 &expr_list->el_exprs);
464 cfs_expr_list_free(expr_list);
470 EXPORT_SYMBOL(cfs_expr_list_parse);
473 * Frees cfs_expr_list structures of \a list.
475 * For each struct cfs_expr_list structure found on \a list it frees
476 * range_expr list attached to it and frees the cfs_expr_list itself.
481 cfs_expr_list_free_list(struct list_head *list)
483 struct cfs_expr_list *el;
485 while (!list_empty(list)) {
486 el = list_entry(list->next,
487 struct cfs_expr_list, el_link);
488 list_del(&el->el_link);
489 cfs_expr_list_free(el);
494 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
496 struct cfs_expr_list *el;
505 while (src.ls_str != NULL) {
508 if (!cfs_gettok(&src, '.', &res)) {
513 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
517 list_add_tail(&el->el_link, list);
526 cfs_expr_list_free_list(list);
530 EXPORT_SYMBOL(cfs_ip_addr_parse);
533 * Matches address (\a addr) against address set encoded in \a list.
535 * \retval 1 if \a addr matches
536 * \retval 0 otherwise
539 cfs_ip_addr_match(__u32 addr, struct list_head *list)
541 struct cfs_expr_list *el;
544 list_for_each_entry_reverse(el, list, el_link) {
545 if (!cfs_expr_list_match(addr & 0xff, el))
553 EXPORT_SYMBOL(cfs_ip_addr_match);
556 cfs_ip_addr_free(struct list_head *list)
558 cfs_expr_list_free_list(list);
560 EXPORT_SYMBOL(cfs_ip_addr_free);