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 /* 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;
74 /* <str> must be a list of tokens separated by whitespace
75 * and optionally an operator ('+' or '-'). If an operator
76 * appears first in <str>, '*oldmask' is used as the starting point
77 * (relative), otherwise minmask is used (absolute). An operator
78 * applies to all following tokens up to the next operator. */
84 if (*str == '+' || *str == '-') {
87 /* only if first token is relative */
91 if (*str == 0) /* trailing op */
95 /* find token length */
96 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
97 str[len] != '+' && str[len] != '-'; len++);
101 for (i = 0; i < 32; i++) {
102 debugstr = bit2str(i);
103 if (debugstr != NULL &&
104 strlen(debugstr) == len &&
105 cfs_strncasecmp(str, debugstr, len) == 0) {
107 newmask &= ~(1 << i);
114 if (!found && len == 3 &&
115 (cfs_strncasecmp(str, "ALL", len) == 0)) {
123 CWARN("unknown mask '%.*s'.\n"
124 "mask usage: [+|-]<all|type> ...\n", len, str);
133 EXPORT_SYMBOL(cfs_str2mask);
135 /* Duplicate a string in a platform-independent way */
136 char *cfs_strdup(const char *str, u_int32_t flags)
138 size_t lenz; /* length of str + zero byte */
141 lenz = strlen(str) + 1;
143 dup_str = cfs_alloc(lenz, flags);
147 memcpy(dup_str, str, lenz);
151 EXPORT_SYMBOL(cfs_strdup);
154 * cfs_{v}snprintf() return the actual size that is printed rather than
155 * the size that would be printed in standard functions.
158 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
163 i = vsnprintf(buf, size, fmt, args);
165 return (i >= size ? size - 1 : i);
167 EXPORT_SYMBOL(cfs_vsnprintf);
170 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
176 i = cfs_vsnprintf(buf, size, fmt, args);
181 EXPORT_SYMBOL(cfs_snprintf);
183 /* get the first string out of @str */
184 char *cfs_firststr(char *str, size_t size)
189 /* trim leading spaces */
190 while (i < size && *str && isspace(*str)) {
195 /* string with all spaces */
200 while (i < size && *end != '\0' && !isspace(*end)) {
209 EXPORT_SYMBOL(cfs_firststr);
212 cfs_trimwhite(char *str)
216 while (cfs_iswhite(*str))
219 end = str + strlen(str);
221 if (!cfs_iswhite(end[-1]))
229 EXPORT_SYMBOL(cfs_trimwhite);
232 * Extracts tokens from strings.
234 * Looks for \a delim in string \a next, sets \a res to point to
235 * substring before the delimiter, sets \a next right after the found
238 * \retval 1 if \a res points to a string of non-whitespace characters
239 * \retval 0 otherwise
242 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
246 if (next->ls_str == NULL)
249 /* skip leading white spaces */
250 while (next->ls_len) {
251 if (!cfs_iswhite(*next->ls_str))
257 if (next->ls_len == 0) /* whitespaces only */
260 if (*next->ls_str == delim) {
261 /* first non-writespace is the delimiter */
265 res->ls_str = next->ls_str;
266 end = memchr(next->ls_str, delim, next->ls_len);
268 /* there is no the delimeter in the string */
269 end = next->ls_str + next->ls_len;
272 next->ls_str = end + 1;
273 next->ls_len -= (end - res->ls_str + 1);
276 /* skip ending whitespaces */
277 while (--end != res->ls_str) {
278 if (!cfs_iswhite(*end))
282 res->ls_len = end - res->ls_str + 1;
285 EXPORT_SYMBOL(cfs_gettok);
288 * Converts string to integer.
290 * Accepts decimal and hexadecimal number recordings.
292 * \retval 1 if first \a nob chars of \a str convert to decimal or
293 * hexadecimal integer in the range [\a min, \a max]
294 * \retval 0 otherwise
297 cfs_str2num_check(char *str, int nob, unsigned *num,
298 unsigned min, unsigned max)
302 str = cfs_trimwhite(str);
303 *num = strtoul(str, &endp, 0);
307 for (; endp < str + nob; endp++) {
308 if (!cfs_iswhite(*endp))
312 return (*num >= min && *num <= max);
314 EXPORT_SYMBOL(cfs_str2num_check);
317 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
318 * \a src should only have a single token which can be \<number\> or \*
320 * \retval pointer to allocated range_expr and initialized
321 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
324 * \<number\> '-' \<number\> |
325 * \<number\> '-' \<number\> '/' \<number\>
326 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
327 * -ENOMEM will be returned.
330 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
331 int bracketed, struct cfs_range_expr **expr)
333 struct cfs_range_expr *re;
336 LIBCFS_ALLOC(re, sizeof(*re));
340 if (src->ls_len == 1 && src->ls_str[0] == '*') {
347 if (cfs_str2num_check(src->ls_str, src->ls_len,
348 &re->re_lo, min, max)) {
349 /* <number> is parsed */
350 re->re_hi = re->re_lo;
355 if (!bracketed || !cfs_gettok(src, '-', &tok))
358 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
359 &re->re_lo, min, max))
363 if (cfs_str2num_check(src->ls_str, src->ls_len,
364 &re->re_hi, min, max)) {
365 /* <number> - <number> is parsed */
370 /* go to check <number> '-' <number> '/' <number> */
371 if (cfs_gettok(src, '/', &tok)) {
372 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
373 &re->re_hi, min, max))
376 /* <number> - <number> / ... */
377 if (cfs_str2num_check(src->ls_str, src->ls_len,
378 &re->re_stride, min, max)) {
379 /* <number> - <number> / <number> is parsed */
389 LIBCFS_FREE(re, sizeof(*re));
392 EXPORT_SYMBOL(cfs_range_expr_parse);
395 * Matches value (\a value) against ranges expression list \a expr_list.
397 * \retval 1 if \a value matches
398 * \retval 0 otherwise
401 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
403 struct cfs_range_expr *expr;
405 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
406 if (value >= expr->re_lo && value <= expr->re_hi &&
407 ((value - expr->re_lo) % expr->re_stride) == 0)
413 EXPORT_SYMBOL(cfs_expr_list_match);
416 * Convert express list (\a expr_list) to an array of all matched values
418 * \retval N N is total number of all matched values
419 * \retval 0 if expression list is empty
420 * \retval < 0 for failure
423 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
425 struct cfs_range_expr *expr;
430 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
431 for (i = expr->re_lo; i <= expr->re_hi; i++) {
432 if (((i - expr->re_lo) % expr->re_stride) == 0)
437 if (count == 0) /* empty expression list */
441 CERROR("Number of values %d exceeds max allowed %d\n",
446 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
451 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
452 for (i = expr->re_lo; i <= expr->re_hi; i++) {
453 if (((i - expr->re_lo) % expr->re_stride) == 0)
461 EXPORT_SYMBOL(cfs_expr_list_values);
464 * Frees cfs_range_expr structures of \a expr_list.
469 cfs_expr_list_free(struct cfs_expr_list *expr_list)
471 while (!cfs_list_empty(&expr_list->el_exprs)) {
472 struct cfs_range_expr *expr;
474 expr = cfs_list_entry(expr_list->el_exprs.next,
475 struct cfs_range_expr, re_link),
476 cfs_list_del(&expr->re_link);
477 LIBCFS_FREE(expr, sizeof(*expr));
480 LIBCFS_FREE(expr_list, sizeof(*expr_list));
482 EXPORT_SYMBOL(cfs_expr_list_free);
485 cfs_expr_list_print(struct cfs_expr_list *expr_list)
487 struct cfs_range_expr *expr;
489 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
490 CDEBUG(D_WARNING, "%d-%d/%d\n",
491 expr->re_lo, expr->re_hi, expr->re_stride);
494 EXPORT_SYMBOL(cfs_expr_list_print);
497 * Parses \<cfs_expr_list\> token of the syntax.
499 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
500 * \retval 0 otherwise
503 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
504 struct cfs_expr_list **elpp)
506 struct cfs_expr_list *expr_list;
507 struct cfs_range_expr *expr;
511 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
512 if (expr_list == NULL)
518 CFS_INIT_LIST_HEAD(&expr_list->el_exprs);
520 if (src.ls_str[0] == '[' &&
521 src.ls_str[src.ls_len - 1] == ']') {
526 while (src.ls_str != NULL) {
529 if (!cfs_gettok(&src, ',', &tok)) {
534 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
538 cfs_list_add_tail(&expr->re_link,
539 &expr_list->el_exprs);
542 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
544 cfs_list_add_tail(&expr->re_link,
545 &expr_list->el_exprs);
550 cfs_expr_list_free(expr_list);
556 EXPORT_SYMBOL(cfs_expr_list_parse);
559 * Frees cfs_expr_list structures of \a list.
561 * For each struct cfs_expr_list structure found on \a list it frees
562 * range_expr list attached to it and frees the cfs_expr_list itself.
567 cfs_expr_list_free_list(cfs_list_t *list)
569 struct cfs_expr_list *el;
571 while (!cfs_list_empty(list)) {
572 el = cfs_list_entry(list->next,
573 struct cfs_expr_list, el_link);
574 cfs_list_del(&el->el_link);
575 cfs_expr_list_free(el);
578 EXPORT_SYMBOL(cfs_expr_list_free_list);
581 cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
583 struct cfs_expr_list *el;
592 while (src.ls_str != NULL) {
595 if (!cfs_gettok(&src, '.', &res)) {
600 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
604 cfs_list_add_tail(&el->el_link, list);
613 cfs_expr_list_free_list(list);
617 EXPORT_SYMBOL(cfs_ip_addr_parse);
620 * Matches address (\a addr) against address set encoded in \a list.
622 * \retval 1 if \a addr matches
623 * \retval 0 otherwise
626 cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
628 struct cfs_expr_list *el;
631 cfs_list_for_each_entry_reverse(el, list, el_link) {
632 if (!cfs_expr_list_match(addr & 0xff, el))
640 EXPORT_SYMBOL(cfs_ip_addr_match);
643 cfs_ip_addr_free(cfs_list_t *list)
645 cfs_expr_list_free_list(list);
647 EXPORT_SYMBOL(cfs_ip_addr_free);