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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * String manipulation functions.
34 * libcfs/libcfs/libcfs_string.c
36 * Author: Nathan Rutman <nathan.rutman@sun.com>
39 #include <linux/ctype.h>
40 #include <libcfs/libcfs.h>
41 #include <libcfs/libcfs_string.h>
43 /* Convert a text string to a bitmask */
44 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
45 int *oldmask, int minmask, int allmask)
49 int newmask = minmask, i, len, found = 0;
52 /* <str> must be a list of tokens separated by whitespace
53 * and optionally an operator ('+' or '-'). If an operator
54 * appears first in <str>, '*oldmask' is used as the starting point
55 * (relative), otherwise minmask is used (absolute). An operator
56 * 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);
112 EXPORT_SYMBOL(cfs_str2mask);
115 * Extracts tokens from strings.
117 * Looks for \a delim in string \a next, sets \a res to point to
118 * substring before the delimiter, sets \a next right after the found
121 * \retval 1 if \a res points to a string of non-whitespace characters
122 * \retval 0 otherwise
125 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
129 if (next->ls_str == NULL)
132 /* skip leading white spaces */
133 while (next->ls_len) {
134 if (!isspace(*next->ls_str))
140 if (next->ls_len == 0) /* whitespaces only */
143 if (*next->ls_str == delim) {
144 /* first non-writespace is the delimiter */
148 res->ls_str = next->ls_str;
149 end = memchr(next->ls_str, delim, next->ls_len);
151 /* there is no the delimeter in the string */
152 end = next->ls_str + next->ls_len;
155 next->ls_str = end + 1;
156 next->ls_len -= (end - res->ls_str + 1);
159 /* skip ending whitespaces */
160 while (--end != res->ls_str) {
165 res->ls_len = end - res->ls_str + 1;
168 EXPORT_SYMBOL(cfs_gettok);
171 * Converts string to integer.
173 * Accepts decimal and hexadecimal number recordings.
175 * \retval 1 if first \a nob chars of \a str convert to decimal or
176 * hexadecimal integer in the range [\a min, \a max]
177 * \retval 0 otherwise
180 cfs_str2num_check(char *str, int nob, unsigned *num,
181 unsigned min, unsigned max)
183 bool all_numbers = true;
190 * kstrouint can only handle strings composed
191 * of only numbers. We need to scan the string
192 * passed in for the first non-digit character
193 * and end the string at that location. If we
194 * don't find any non-digit character we still
195 * need to place a '\0' at position len since
196 * we are not interested in the rest of the
197 * string which is longer than len in size.
198 * After we are done the character at the
199 * position we placed '\0' must be restored.
201 len = min((int)strlen(endp), nob);
202 for (; endp < str + len; endp++) {
203 if (!isxdigit(*endp) && *endp != '-' &&
210 /* Eat trailing space */
211 if (!all_numbers && isspace(*endp)) {
219 rc = kstrtouint(str, 0, num);
221 if (rc || !all_numbers)
224 return (*num >= min && *num <= max);
226 EXPORT_SYMBOL(cfs_str2num_check);
229 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
230 * \a src should only have a single token which can be \<number\> or \*
232 * \retval pointer to allocated range_expr and initialized
233 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
236 * \<number\> '-' \<number\> |
237 * \<number\> '-' \<number\> '/' \<number\>
238 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
239 * -ENOMEM will be returned.
242 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
243 int bracketed, struct cfs_range_expr **expr)
245 struct cfs_range_expr *re;
248 LIBCFS_ALLOC(re, sizeof(*re));
252 if (src->ls_len == 1 && src->ls_str[0] == '*') {
259 if (cfs_str2num_check(src->ls_str, src->ls_len,
260 &re->re_lo, min, max)) {
261 /* <number> is parsed */
262 re->re_hi = re->re_lo;
267 if (!bracketed || !cfs_gettok(src, '-', &tok))
270 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
271 &re->re_lo, min, max))
275 if (cfs_str2num_check(src->ls_str, src->ls_len,
276 &re->re_hi, min, max)) {
277 /* <number> - <number> is parsed */
282 /* go to check <number> '-' <number> '/' <number> */
283 if (cfs_gettok(src, '/', &tok)) {
284 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
285 &re->re_hi, min, max))
288 /* <number> - <number> / ... */
289 if (cfs_str2num_check(src->ls_str, src->ls_len,
290 &re->re_stride, min, max)) {
291 /* <number> - <number> / <number> is parsed */
301 LIBCFS_FREE(re, sizeof(*re));
306 * Print the range expression \a re into specified \a buffer.
307 * If \a bracketed is true, expression does not need additional
310 * \retval number of characters written
313 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
323 if (expr->re_lo == expr->re_hi)
324 i = scnprintf(buffer, count, "%u", expr->re_lo);
325 else if (expr->re_stride == 1)
326 i = scnprintf(buffer, count, "%s%u-%u%s",
327 s, expr->re_lo, expr->re_hi, e);
329 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
330 s, expr->re_lo, expr->re_hi,
336 * Print a list of range expressions (\a expr_list) into specified \a buffer.
337 * If the list contains several expressions, separate them with comma
338 * and surround the list with brackets.
340 * \retval number of characters written
343 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
345 struct cfs_range_expr *expr;
352 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
356 i += scnprintf(buffer + i, count - i, "[");
358 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
360 i += scnprintf(buffer + i, count - i, ",");
361 i += cfs_range_expr_print(buffer + i, count - i, expr,
366 i += scnprintf(buffer + i, count - i, "]");
370 EXPORT_SYMBOL(cfs_expr_list_print);
373 * Matches value (\a value) against ranges expression list \a expr_list.
375 * \retval 1 if \a value matches
376 * \retval 0 otherwise
379 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
381 struct cfs_range_expr *expr;
383 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
384 if (value >= expr->re_lo && value <= expr->re_hi &&
385 ((value - expr->re_lo) % expr->re_stride) == 0)
391 EXPORT_SYMBOL(cfs_expr_list_match);
394 * Convert express list (\a expr_list) to an array of all matched values
396 * \retval N N is total number of all matched values
397 * \retval 0 if expression list is empty
398 * \retval < 0 for failure
401 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
403 struct cfs_range_expr *expr;
408 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
409 for (i = expr->re_lo; i <= expr->re_hi; i++) {
410 if (((i - expr->re_lo) % expr->re_stride) == 0)
415 if (count == 0) /* empty expression list */
419 CERROR("Number of values %d exceeds max allowed %d\n",
424 CFS_ALLOC_PTR_ARRAY(val, count);
429 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)
439 EXPORT_SYMBOL(cfs_expr_list_values);
442 cfs_expr_list_values_free(__u32 *values, int num)
444 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
445 * by OBD_FREE() if it's called by module other than libcfs & LNet,
446 * otherwise we will see fake memory leak */
447 CFS_FREE_PTR_ARRAY(values, num);
449 EXPORT_SYMBOL(cfs_expr_list_values_free);
452 * Frees cfs_range_expr structures of \a expr_list.
457 cfs_expr_list_free(struct cfs_expr_list *expr_list)
459 while (!list_empty(&expr_list->el_exprs)) {
460 struct cfs_range_expr *expr;
462 expr = list_entry(expr_list->el_exprs.next,
463 struct cfs_range_expr, re_link);
464 list_del(&expr->re_link);
465 LIBCFS_FREE(expr, sizeof(*expr));
468 LIBCFS_FREE(expr_list, sizeof(*expr_list));
470 EXPORT_SYMBOL(cfs_expr_list_free);
473 * Parses \<cfs_expr_list\> token of the syntax.
475 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
476 * \retval -errno otherwise
479 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
480 struct cfs_expr_list **elpp)
482 struct cfs_expr_list *expr_list;
483 struct cfs_range_expr *expr;
487 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
488 if (expr_list == NULL)
494 INIT_LIST_HEAD(&expr_list->el_exprs);
496 if (src.ls_str[0] == '[' &&
497 src.ls_str[src.ls_len - 1] == ']') {
502 while (src.ls_str != NULL) {
505 if (!cfs_gettok(&src, ',', &tok)) {
510 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
514 list_add_tail(&expr->re_link, &expr_list->el_exprs);
517 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
519 list_add_tail(&expr->re_link, &expr_list->el_exprs);
523 cfs_expr_list_free(expr_list);
529 EXPORT_SYMBOL(cfs_expr_list_parse);
532 * Frees cfs_expr_list structures of \a list.
534 * For each struct cfs_expr_list structure found on \a list it frees
535 * range_expr list attached to it and frees the cfs_expr_list itself.
540 cfs_expr_list_free_list(struct list_head *list)
542 struct cfs_expr_list *el;
544 while (!list_empty(list)) {
545 el = list_entry(list->next,
546 struct cfs_expr_list, el_link);
547 list_del(&el->el_link);
548 cfs_expr_list_free(el);
551 EXPORT_SYMBOL(cfs_expr_list_free_list);