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>
43 #include <libcfs/libcfs.h>
45 /* non-0 = don't match */
46 static 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);
64 /* Convert a text string to a bitmask */
65 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
66 int *oldmask, int minmask, int allmask)
70 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 /* Duplicate a string in a platform-independent way */
135 char *cfs_strdup(const char *str, u_int32_t flags)
137 size_t lenz; /* length of str + zero byte */
140 lenz = strlen(str) + 1;
142 dup_str = cfs_alloc(lenz, flags);
146 memcpy(dup_str, str, lenz);
150 EXPORT_SYMBOL(cfs_strdup);
153 * cfs_{v}snprintf() return the actual size that is printed rather than
154 * the size that would be printed in standard functions.
157 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
162 i = vsnprintf(buf, size, fmt, args);
164 return (i >= size ? size - 1 : i);
166 EXPORT_SYMBOL(cfs_vsnprintf);
169 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
175 i = cfs_vsnprintf(buf, size, fmt, args);
180 EXPORT_SYMBOL(cfs_snprintf);
182 /* get the first string out of @str */
183 char *cfs_firststr(char *str, size_t size)
188 /* trim leading spaces */
189 while (i < size && *str && isspace(*str)) {
194 /* string with all spaces */
199 while (i < size && *end != '\0' && !isspace(*end)) {
208 EXPORT_SYMBOL(cfs_firststr);
211 cfs_trimwhite(char *str)
215 while (cfs_iswhite(*str))
218 end = str + strlen(str);
220 if (!cfs_iswhite(end[-1]))
228 EXPORT_SYMBOL(cfs_trimwhite);
231 * Extracts tokens from strings.
233 * Looks for \a delim in string \a next, sets \a res to point to
234 * substring before the delimiter, sets \a next right after the found
237 * \retval 1 if \a res points to a string of non-whitespace characters
238 * \retval 0 otherwise
241 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
245 if (next->ls_str == NULL)
248 /* skip leading white spaces */
249 while (next->ls_len) {
250 if (!cfs_iswhite(*next->ls_str))
256 if (next->ls_len == 0) /* whitespaces only */
259 if (*next->ls_str == delim) {
260 /* first non-writespace is the delimiter */
264 res->ls_str = next->ls_str;
265 end = memchr(next->ls_str, delim, next->ls_len);
267 /* there is no the delimeter in the string */
268 end = next->ls_str + next->ls_len;
271 next->ls_str = end + 1;
272 next->ls_len -= (end - res->ls_str + 1);
275 /* skip ending whitespaces */
276 while (--end != res->ls_str) {
277 if (!cfs_iswhite(*end))
281 res->ls_len = end - res->ls_str + 1;
284 EXPORT_SYMBOL(cfs_gettok);
287 * Converts string to integer.
289 * Accepts decimal and hexadecimal number recordings.
291 * \retval 1 if first \a nob chars of \a str convert to decimal or
292 * hexadecimal integer in the range [\a min, \a max]
293 * \retval 0 otherwise
296 cfs_str2num_check(char *str, int nob, unsigned *num,
297 unsigned min, unsigned max)
301 str = cfs_trimwhite(str);
302 *num = strtoul(str, &endp, 0);
306 for (; endp < str + nob; endp++) {
307 if (!cfs_iswhite(*endp))
311 return (*num >= min && *num <= max);
313 EXPORT_SYMBOL(cfs_str2num_check);
316 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
317 * \a src should only have a single token which can be \<number\> or \*
319 * \retval pointer to allocated range_expr and initialized
320 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
323 * \<number\> '-' \<number\> |
324 * \<number\> '-' \<number\> '/' \<number\>
325 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
326 * -ENOMEM will be returned.
329 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
330 int bracketed, struct cfs_range_expr **expr)
332 struct cfs_range_expr *re;
335 LIBCFS_ALLOC(re, sizeof(*re));
339 if (src->ls_len == 1 && src->ls_str[0] == '*') {
346 if (cfs_str2num_check(src->ls_str, src->ls_len,
347 &re->re_lo, min, max)) {
348 /* <number> is parsed */
349 re->re_hi = re->re_lo;
354 if (!bracketed || !cfs_gettok(src, '-', &tok))
357 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
358 &re->re_lo, min, max))
362 if (cfs_str2num_check(src->ls_str, src->ls_len,
363 &re->re_hi, min, max)) {
364 /* <number> - <number> is parsed */
369 /* go to check <number> '-' <number> '/' <number> */
370 if (cfs_gettok(src, '/', &tok)) {
371 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
372 &re->re_hi, min, max))
375 /* <number> - <number> / ... */
376 if (cfs_str2num_check(src->ls_str, src->ls_len,
377 &re->re_stride, min, max)) {
378 /* <number> - <number> / <number> is parsed */
388 LIBCFS_FREE(re, sizeof(*re));
391 EXPORT_SYMBOL(cfs_range_expr_parse);
394 * Matches value (\a value) against ranges expression list \a expr_list.
396 * \retval 1 if \a value matches
397 * \retval 0 otherwise
400 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
402 struct cfs_range_expr *expr;
404 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
405 if (value >= expr->re_lo && value <= expr->re_hi &&
406 ((value - expr->re_lo) % expr->re_stride) == 0)
412 EXPORT_SYMBOL(cfs_expr_list_match);
415 * Convert express list (\a expr_list) to an array of all matched values
417 * \retval N N is total number of all matched values
418 * \retval 0 if expression list is empty
419 * \retval < 0 for failure
422 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
424 struct cfs_range_expr *expr;
429 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
430 for (i = expr->re_lo; i <= expr->re_hi; i++) {
431 if (((i - expr->re_lo) % expr->re_stride) == 0)
436 if (count == 0) /* empty expression list */
440 CERROR("Number of values %d exceeds max allowed %d\n",
445 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
450 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
451 for (i = expr->re_lo; i <= expr->re_hi; i++) {
452 if (((i - expr->re_lo) % expr->re_stride) == 0)
460 EXPORT_SYMBOL(cfs_expr_list_values);
463 * Frees cfs_range_expr structures of \a expr_list.
468 cfs_expr_list_free(struct cfs_expr_list *expr_list)
470 while (!cfs_list_empty(&expr_list->el_exprs)) {
471 struct cfs_range_expr *expr;
473 expr = cfs_list_entry(expr_list->el_exprs.next,
474 struct cfs_range_expr, re_link),
475 cfs_list_del(&expr->re_link);
476 LIBCFS_FREE(expr, sizeof(*expr));
479 LIBCFS_FREE(expr_list, sizeof(*expr_list));
481 EXPORT_SYMBOL(cfs_expr_list_free);
484 cfs_expr_list_print(struct cfs_expr_list *expr_list)
486 struct cfs_range_expr *expr;
488 cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
489 CDEBUG(D_WARNING, "%d-%d/%d\n",
490 expr->re_lo, expr->re_hi, expr->re_stride);
493 EXPORT_SYMBOL(cfs_expr_list_print);
496 * Parses \<cfs_expr_list\> token of the syntax.
498 * \retval 1 if \a str parses to \<number\> | \<expr_list\>
499 * \retval 0 otherwise
502 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
503 struct cfs_expr_list **elpp)
505 struct cfs_expr_list *expr_list;
506 struct cfs_range_expr *expr;
510 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
511 if (expr_list == NULL)
517 CFS_INIT_LIST_HEAD(&expr_list->el_exprs);
519 if (src.ls_str[0] == '[' &&
520 src.ls_str[src.ls_len - 1] == ']') {
525 while (src.ls_str != NULL) {
528 if (!cfs_gettok(&src, ',', &tok)) {
533 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
537 cfs_list_add_tail(&expr->re_link,
538 &expr_list->el_exprs);
541 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
543 cfs_list_add_tail(&expr->re_link,
544 &expr_list->el_exprs);
549 cfs_expr_list_free(expr_list);
555 EXPORT_SYMBOL(cfs_expr_list_parse);
558 * Frees cfs_expr_list structures of \a list.
560 * For each struct cfs_expr_list structure found on \a list it frees
561 * range_expr list attached to it and frees the cfs_expr_list itself.
566 cfs_expr_list_free_list(cfs_list_t *list)
568 struct cfs_expr_list *el;
570 while (!cfs_list_empty(list)) {
571 el = cfs_list_entry(list->next,
572 struct cfs_expr_list, el_link);
573 cfs_list_del(&el->el_link);
574 cfs_expr_list_free(el);
577 EXPORT_SYMBOL(cfs_expr_list_free_list);
580 cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
582 struct cfs_expr_list *el;
591 while (src.ls_str != NULL) {
594 if (!cfs_gettok(&src, '.', &res)) {
599 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
603 cfs_list_add_tail(&el->el_link, list);
612 cfs_expr_list_free_list(list);
616 EXPORT_SYMBOL(cfs_ip_addr_parse);
619 * Matches address (\a addr) against address set encoded in \a list.
621 * \retval 1 if \a addr matches
622 * \retval 0 otherwise
625 cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
627 struct cfs_expr_list *el;
630 cfs_list_for_each_entry_reverse(el, list, el_link) {
631 if (!cfs_expr_list_match(addr & 0xff, el))
639 EXPORT_SYMBOL(cfs_ip_addr_match);
642 cfs_ip_addr_free(cfs_list_t *list)
644 cfs_expr_list_free_list(list);
646 EXPORT_SYMBOL(cfs_ip_addr_free);