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/
31 * String manipulation functions.
33 * libcfs/libcfs/libcfs_string.c
35 * Author: Nathan Rutman <nathan.rutman@sun.com>
38 #include <linux/ctype.h>
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_string.h>
42 /* Convert a text string to a bitmask */
43 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
44 int *oldmask, int minmask, int allmask)
48 int newmask = minmask, i, len, found = 0;
51 /* <str> must be a list of tokens separated by whitespace or comma,
52 * and optionally an operator ('+' or '-'). If an operator
53 * appears first in <str>, '*oldmask' is used as the starting point
54 * (relative), otherwise minmask is used (absolute). An operator
55 * applies to all following tokens up to the next operator.
58 while (isspace(*str) || *str == ',')
62 if (*str == '+' || *str == '-') {
65 /* only if first token is relative */
69 if (*str == 0) /* trailing op */
73 /* find token length */
74 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
75 str[len] != '+' && str[len] != '-' && str[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;
156 next->ls_str = end + 1;
157 next->ls_len -= (end - res->ls_str + 1);
160 /* skip ending whitespaces */
161 while (--end != res->ls_str) {
166 res->ls_len = end - res->ls_str + 1;
169 EXPORT_SYMBOL(cfs_gettok);
172 * Converts string to integer.
174 * Accepts decimal and hexadecimal number recordings.
176 * \retval 1 if first \a nob chars of \a str convert to decimal or
177 * hexadecimal integer in the range [\a min, \a max]
178 * \retval 0 otherwise
181 cfs_str2num_check(char *str, int nob, unsigned *num,
182 unsigned min, unsigned max)
184 bool all_numbers = true;
191 * kstrouint can only handle strings composed
192 * of only numbers. We need to scan the string
193 * passed in for the first non-digit character
194 * and end the string at that location. If we
195 * don't find any non-digit character we still
196 * need to place a '\0' at position len since
197 * we are not interested in the rest of the
198 * string which is longer than len in size.
199 * After we are done the character at the
200 * position we placed '\0' must be restored.
202 len = min((int)strlen(endp), nob);
203 for (; endp < str + len; endp++) {
204 if (!isxdigit(*endp) && *endp != '-' &&
211 /* Eat trailing space */
212 if (!all_numbers && isspace(*endp)) {
220 rc = kstrtouint(str, 0, num);
222 if (rc || !all_numbers)
225 return (*num >= min && *num <= max);
227 EXPORT_SYMBOL(cfs_str2num_check);
230 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
231 * \a src should only have a single token which can be \<number\> or \*
233 * \retval pointer to allocated range_expr and initialized
234 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
237 * \<number\> '-' \<number\> |
238 * \<number\> '-' \<number\> '/' \<number\>
239 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
240 * -ENOMEM will be returned.
243 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
244 int bracketed, struct cfs_range_expr **expr)
246 struct cfs_range_expr *re;
249 LIBCFS_ALLOC(re, sizeof(*re));
253 if (src->ls_len == 1 && src->ls_str[0] == '*') {
260 if (cfs_str2num_check(src->ls_str, src->ls_len,
261 &re->re_lo, min, max)) {
262 /* <number> is parsed */
263 re->re_hi = re->re_lo;
268 if (!bracketed || !cfs_gettok(src, '-', &tok))
271 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
272 &re->re_lo, min, max))
276 if (cfs_str2num_check(src->ls_str, src->ls_len,
277 &re->re_hi, min, max)) {
278 /* <number> - <number> is parsed */
283 /* go to check <number> '-' <number> '/' <number> */
284 if (cfs_gettok(src, '/', &tok)) {
285 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
286 &re->re_hi, min, max))
289 /* <number> - <number> / ... */
290 if (cfs_str2num_check(src->ls_str, src->ls_len,
291 &re->re_stride, min, max)) {
292 /* <number> - <number> / <number> is parsed */
302 LIBCFS_FREE(re, sizeof(*re));
307 * Print the range expression \a re into specified \a buffer.
308 * If \a bracketed is true, expression does not need additional
311 * \retval number of characters written
314 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
324 if (expr->re_lo == expr->re_hi)
325 i = scnprintf(buffer, count, "%u", expr->re_lo);
326 else if (expr->re_stride == 1)
327 i = scnprintf(buffer, count, "%s%u-%u%s",
328 s, expr->re_lo, expr->re_hi, e);
330 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
331 s, expr->re_lo, expr->re_hi,
337 * Print a list of range expressions (\a expr_list) into specified \a buffer.
338 * If the list contains several expressions, separate them with comma
339 * and surround the list with brackets.
341 * \retval number of characters written
344 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
346 struct cfs_range_expr *expr;
353 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
357 i += scnprintf(buffer + i, count - i, "[");
359 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
361 i += scnprintf(buffer + i, count - i, ",");
362 i += cfs_range_expr_print(buffer + i, count - i, expr,
367 i += scnprintf(buffer + i, count - i, "]");
371 EXPORT_SYMBOL(cfs_expr_list_print);
374 * Matches value (\a value) against ranges expression list \a expr_list.
376 * \retval 1 if \a value matches
377 * \retval 0 otherwise
380 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
382 struct cfs_range_expr *expr;
384 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
385 if (value >= expr->re_lo && value <= expr->re_hi &&
386 ((value - expr->re_lo) % expr->re_stride) == 0)
392 EXPORT_SYMBOL(cfs_expr_list_match);
395 * Convert express list (\a expr_list) to an array of all matched values
397 * \retval N N is total number of all matched values
398 * \retval 0 if expression list is empty
399 * \retval < 0 for failure
402 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
404 struct cfs_range_expr *expr;
409 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
410 for (i = expr->re_lo; i <= expr->re_hi; i++) {
411 if (((i - expr->re_lo) % expr->re_stride) == 0)
416 if (count == 0) /* empty expression list */
420 CERROR("Number of values %d exceeds max allowed %d\n",
425 CFS_ALLOC_PTR_ARRAY(val, count);
430 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)
440 EXPORT_SYMBOL(cfs_expr_list_values);
443 cfs_expr_list_values_free(__u32 *values, int num)
445 /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
446 * by OBD_FREE() if it's called by module other than libcfs & LNet,
447 * otherwise we will see fake memory leak */
448 CFS_FREE_PTR_ARRAY(values, num);
450 EXPORT_SYMBOL(cfs_expr_list_values_free);
453 * Frees cfs_range_expr structures of \a expr_list.
458 cfs_expr_list_free(struct cfs_expr_list *expr_list)
460 while (!list_empty(&expr_list->el_exprs)) {
461 struct cfs_range_expr *expr;
463 expr = list_entry(expr_list->el_exprs.next,
464 struct cfs_range_expr, re_link);
465 list_del(&expr->re_link);
466 LIBCFS_FREE(expr, sizeof(*expr));
469 LIBCFS_FREE(expr_list, sizeof(*expr_list));
471 EXPORT_SYMBOL(cfs_expr_list_free);
474 * Parses \<cfs_expr_list\> token of the syntax.
476 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
477 * \retval -errno otherwise
480 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
481 struct cfs_expr_list **elpp)
483 struct cfs_expr_list *expr_list;
484 struct cfs_range_expr *expr;
488 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
489 if (expr_list == NULL)
495 INIT_LIST_HEAD(&expr_list->el_exprs);
497 if (src.ls_str[0] == '[' &&
498 src.ls_str[src.ls_len - 1] == ']') {
503 while (src.ls_str != NULL) {
506 if (!cfs_gettok(&src, ',', &tok)) {
511 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
515 list_add_tail(&expr->re_link, &expr_list->el_exprs);
518 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
520 list_add_tail(&expr->re_link, &expr_list->el_exprs);
524 cfs_expr_list_free(expr_list);
530 EXPORT_SYMBOL(cfs_expr_list_parse);
533 * Frees cfs_expr_list structures of \a list.
535 * For each struct cfs_expr_list structure found on \a list it frees
536 * range_expr list attached to it and frees the cfs_expr_list itself.
541 cfs_expr_list_free_list(struct list_head *list)
543 struct cfs_expr_list *el;
545 while (!list_empty(list)) {
546 el = list_entry(list->next,
547 struct cfs_expr_list, el_link);
548 list_del(&el->el_link);
549 cfs_expr_list_free(el);
552 EXPORT_SYMBOL(cfs_expr_list_free_list);