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, Whamcloud, Inc.
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>
44 # define EXPORT_SYMTAB
47 #include <libcfs/libcfs.h>
49 /* non-0 = don't match */
50 static int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
52 if (s1 == NULL || s2 == NULL)
58 while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
59 if (n == 0 || *s1 == '\0' || *s2 == '\0')
65 return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
68 /* Convert a text string to a bitmask */
69 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
70 int *oldmask, int minmask, int allmask)
74 int newmask = minmask, i, len, found = 0;
77 /* <str> must be a list of tokens separated by whitespace
78 * and optionally an operator ('+' or '-'). If an operator
79 * appears first in <str>, '*oldmask' is used as the starting point
80 * (relative), otherwise minmask is used (absolute). An operator
81 * applies to all following tokens up to the next operator. */
87 if (*str == '+' || *str == '-') {
90 /* only if first token is relative */
94 if (*str == 0) /* trailing op */
98 /* find token length */
99 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
100 str[len] != '+' && str[len] != '-'; len++);
104 for (i = 0; i < 32; i++) {
105 debugstr = bit2str(i);
106 if (debugstr != NULL &&
107 strlen(debugstr) == len &&
108 cfs_strncasecmp(str, debugstr, len) == 0) {
110 newmask &= ~(1 << i);
117 if (!found && len == 3 &&
118 (cfs_strncasecmp(str, "ALL", len) == 0)) {
126 CWARN("unknown mask '%.*s'.\n"
127 "mask usage: [+|-]<all|type> ...\n", len, str);
136 EXPORT_SYMBOL(cfs_str2mask);
138 /* Duplicate a string in a platform-independent way */
139 char *cfs_strdup(const char *str, u_int32_t flags)
141 size_t lenz; /* length of str + zero byte */
144 lenz = strlen(str) + 1;
146 dup_str = cfs_alloc(lenz, flags);
150 memcpy(dup_str, str, lenz);
154 EXPORT_SYMBOL(cfs_strdup);
157 * cfs_{v}snprintf() return the actual size that is printed rather than
158 * the size that would be printed in standard functions.
161 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
166 i = vsnprintf(buf, size, fmt, args);
168 return (i >= size ? size - 1 : i);
170 EXPORT_SYMBOL(cfs_vsnprintf);
173 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
179 i = cfs_vsnprintf(buf, size, fmt, args);
184 EXPORT_SYMBOL(cfs_snprintf);
186 /* get the first string out of @str */
187 char *cfs_firststr(char *str, size_t size)
192 /* trim leading spaces */
193 while (i < size && *str && isspace(*str)) {
198 /* string with all spaces */
203 while (i < size && *end != '\0' && !isspace(*end)) {
212 EXPORT_SYMBOL(cfs_firststr);
215 cfs_trimwhite(char *str)
219 while (cfs_iswhite(*str))
222 end = str + strlen(str);
224 if (!cfs_iswhite(end[-1]))
232 EXPORT_SYMBOL(cfs_trimwhite);
235 * Extracts tokens from strings.
237 * Looks for \a delim in string \a next, sets \a res to point to
238 * substring before the delimiter, sets \a next right after the found
241 * \retval 1 if \a res points to a string of non-whitespace characters
242 * \retval 0 otherwise
245 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
249 if (next->ls_str == NULL)
252 /* skip leading white spaces */
253 while (next->ls_len) {
254 if (!cfs_iswhite(*next->ls_str))
260 if (next->ls_len == 0) /* whitespaces only */
263 if (*next->ls_str == delim) {
264 /* first non-writespace is the delimiter */
268 res->ls_str = next->ls_str;
269 end = memchr(next->ls_str, delim, next->ls_len);
271 /* there is no the delimeter in the string */
272 end = next->ls_str + next->ls_len;
275 next->ls_str = end + 1;
276 next->ls_len -= (end - res->ls_str + 1);
279 /* skip ending whitespaces */
280 while (--end != res->ls_str) {
281 if (!cfs_iswhite(*end))
285 res->ls_len = end - res->ls_str + 1;
288 EXPORT_SYMBOL(cfs_gettok);
291 * Converts string to integer.
293 * Accepts decimal and hexadecimal number recordings.
295 * \retval 1 if first \a nob chars of \a str convert to decimal or
296 * hexadecimal integer in the range [\a min, \a max]
297 * \retval 0 otherwise
300 cfs_str2num_check(char *str, int nob, unsigned *num,
301 unsigned min, unsigned max)
305 str = cfs_trimwhite(str);
306 *num = strtoul(str, &endp, 0);
310 for (; endp < str + nob; endp++) {
311 if (!cfs_iswhite(*endp))
315 return (*num >= min && *num <= max);
317 EXPORT_SYMBOL(cfs_str2num_check);
320 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
321 * \a src should only have a single token which can be \<number\> or \*
323 * \retval pointer to allocated range_expr and initialized
324 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
327 * \<number\> '-' \<number\> |
328 * \<number\> '-' \<number\> '/' \<number\>
329 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
330 * -ENOMEM will be returned.
333 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
334 int bracketed, struct cfs_range_expr **expr)
336 struct cfs_range_expr *re;
339 LIBCFS_ALLOC(re, sizeof(*re));
343 if (src->ls_len == 1 && src->ls_str[0] == '*') {
350 if (cfs_str2num_check(src->ls_str, src->ls_len,
351 &re->re_lo, min, max)) {
352 /* <number> is parsed */
353 re->re_hi = re->re_lo;
358 if (!bracketed || !cfs_gettok(src, '-', &tok))
361 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
362 &re->re_lo, min, max))
366 if (cfs_str2num_check(src->ls_str, src->ls_len,
367 &re->re_hi, min, max)) {
368 /* <number> - <number> is parsed */
373 /* go to check <number> '-' <number> '/' <number> */
374 if (cfs_gettok(src, '/', &tok)) {
375 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
376 &re->re_hi, min, max))
379 /* <number> - <number> / ... */
380 if (cfs_str2num_check(src->ls_str, src->ls_len,
381 &re->re_stride, min, max)) {
382 /* <number> - <number> / <number> is parsed */
392 LIBCFS_FREE(re, sizeof(*re));
395 EXPORT_SYMBOL(cfs_range_expr_parse);
398 * Matches value (\a value) against ranges expression list \a expr_list.
400 * \retval 1 if \a value matches
401 * \retval 0 otherwise
404 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
406 struct cfs_range_expr *expr;
408 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
409 if (value >= expr->re_lo && value <= expr->re_hi &&
410 ((value - expr->re_lo) % expr->re_stride) == 0)
416 EXPORT_SYMBOL(cfs_expr_list_match);
419 * Convert express list (\a expr_list) to an array of all matched values
421 * \retval N N is total number of all matched values
422 * \retval 0 if expression list is empty
423 * \retval < 0 for failure
426 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
428 struct cfs_range_expr *expr;
433 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
434 for (i = expr->re_lo; i <= expr->re_hi; i++) {
435 if (((i - expr->re_lo) % expr->re_stride) == 0)
440 if (count == 0) /* empty expression list */
444 CERROR("Number of values %d exceeds max allowed %d\n",
449 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
454 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
455 for (i = expr->re_lo; i <= expr->re_hi; i++) {
456 if (((i - expr->re_lo) % expr->re_stride) == 0)
464 EXPORT_SYMBOL(cfs_expr_list_values);
467 * Frees cfs_range_expr structures of \a expr_list.
472 cfs_expr_list_free(struct cfs_expr_list *expr_list)
474 while (!cfs_list_empty(&expr_list->el_exprs)) {
475 struct cfs_range_expr *expr;
477 expr = cfs_list_entry(expr_list->el_exprs.next,
478 struct cfs_range_expr, re_link),
479 cfs_list_del(&expr->re_link);
480 LIBCFS_FREE(expr, sizeof(*expr));
483 LIBCFS_FREE(expr_list, sizeof(*expr_list));
485 EXPORT_SYMBOL(cfs_expr_list_free);
488 cfs_expr_list_print(struct cfs_expr_list *expr_list)
490 struct cfs_range_expr *expr;
492 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
493 CDEBUG(D_WARNING, "%d-%d/%d\n",
494 expr->re_lo, expr->re_hi, expr->re_stride);
497 EXPORT_SYMBOL(cfs_expr_list_print);
500 * Parses \<cfs_expr_list\> token of the syntax.
502 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
503 * \retval 0 otherwise
506 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
507 struct cfs_expr_list **elpp)
509 struct cfs_expr_list *expr_list;
510 struct cfs_range_expr *expr;
514 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
515 if (expr_list == NULL)
521 CFS_INIT_LIST_HEAD(&expr_list->el_exprs);
523 if (src.ls_str[0] == '[' &&
524 src.ls_str[src.ls_len - 1] == ']') {
529 while (src.ls_str != NULL) {
532 if (!cfs_gettok(&src, ',', &tok)) {
537 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
541 cfs_list_add_tail(&expr->re_link,
542 &expr_list->el_exprs);
545 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
547 cfs_list_add_tail(&expr->re_link,
548 &expr_list->el_exprs);
553 cfs_expr_list_free(expr_list);
559 EXPORT_SYMBOL(cfs_expr_list_parse);
562 * Frees cfs_expr_list structures of \a list.
564 * For each struct cfs_expr_list structure found on \a list it frees
565 * range_expr list attached to it and frees the cfs_expr_list itself.
570 cfs_expr_list_free_list(cfs_list_t *list)
572 struct cfs_expr_list *el;
574 while (!cfs_list_empty(list)) {
575 el = cfs_list_entry(list->next,
576 struct cfs_expr_list, el_link);
577 cfs_list_del(&el->el_link);
578 cfs_expr_list_free(el);
581 EXPORT_SYMBOL(cfs_expr_list_free_list);
584 cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
586 struct cfs_expr_list *el;
595 while (src.ls_str != NULL) {
598 if (!cfs_gettok(&src, '.', &res)) {
603 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
607 cfs_list_add_tail(&el->el_link, list);
616 cfs_expr_list_free_list(list);
620 EXPORT_SYMBOL(cfs_ip_addr_parse);
623 * Matches address (\a addr) against address set encoded in \a list.
625 * \retval 1 if \a addr matches
626 * \retval 0 otherwise
629 cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
631 struct cfs_expr_list *el;
634 cfs_list_for_each_entry_reverse(el, list, el_link) {
635 if (!cfs_expr_list_match(addr & 0xff, el))
643 EXPORT_SYMBOL(cfs_ip_addr_match);
646 cfs_ip_addr_free(cfs_list_t *list)
648 cfs_expr_list_free_list(list);
650 EXPORT_SYMBOL(cfs_ip_addr_free);