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, 2015 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 "../../include/linux/libcfs/libcfs.h"
41 /* Convert a text string to a bitmask */
42 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
43 int *oldmask, int minmask, int allmask)
47 int newmask = minmask, i, len, found = 0;
49 /* <str> must be a list of tokens separated by whitespace
50 * and optionally an operator ('+' or '-'). If an operator
51 * appears first in <str>, '*oldmask' is used as the starting point
52 * (relative), otherwise minmask is used (absolute). An operator
53 * applies to all following tokens up to the next operator.
55 while (*str != '\0') {
60 if (*str == '+' || *str == '-') {
63 /* only if first token is relative */
67 if (*str == '\0') /* trailing op */
71 /* find token length */
73 while (str[len] != '\0' && !isspace(str[len]) &&
74 str[len] != '+' && str[len] != '-')
79 for (i = 0; i < 32; i++) {
80 debugstr = bit2str(i);
81 if (debugstr && strlen(debugstr) == len &&
82 strncasecmp(str, debugstr, len) == 0) {
91 if (!found && len == 3 &&
92 (strncasecmp(str, "ALL", len) == 0)) {
100 CWARN("unknown mask '%.*s'.\n"
101 "mask usage: [+|-]<all|type> ...\n", len, str);
111 /* get the first string out of @str */
112 char *cfs_firststr(char *str, size_t size)
117 /* trim leading spaces */
118 while (i < size && *str && isspace(*str)) {
123 /* string with all spaces */
128 while (i < size && *end != '\0' && !isspace(*end)) {
137 EXPORT_SYMBOL(cfs_firststr);
140 cfs_trimwhite(char *str)
144 while (isspace(*str))
147 end = str + strlen(str);
149 if (!isspace(end[-1]))
157 EXPORT_SYMBOL(cfs_trimwhite);
160 * Extracts tokens from strings.
162 * Looks for \a delim in string \a next, sets \a res to point to
163 * substring before the delimiter, sets \a next right after the found
166 * \retval 1 if \a res points to a string of non-whitespace characters
167 * \retval 0 otherwise
170 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
177 /* skip leading white spaces */
178 while (next->ls_len) {
179 if (!isspace(*next->ls_str))
185 if (next->ls_len == 0) /* whitespaces only */
188 if (*next->ls_str == delim) {
189 /* first non-writespace is the delimiter */
193 res->ls_str = next->ls_str;
194 end = memchr(next->ls_str, delim, next->ls_len);
196 /* there is no the delimeter in the string */
197 end = next->ls_str + next->ls_len;
200 next->ls_str = end + 1;
201 next->ls_len -= (end - res->ls_str + 1);
204 /* skip ending whitespaces */
205 while (--end != res->ls_str) {
210 res->ls_len = end - res->ls_str + 1;
213 EXPORT_SYMBOL(cfs_gettok);
216 * Converts string to integer.
218 * Accepts decimal and hexadecimal number recordings.
220 * \retval 1 if first \a nob chars of \a str convert to decimal or
221 * hexadecimal integer in the range [\a min, \a max]
222 * \retval 0 otherwise
225 cfs_str2num_check(char *str, int nob, unsigned *num,
226 unsigned min, unsigned max)
228 bool all_numbers = true;
233 * kstrouint can only handle strings composed
234 * of only numbers. We need to scan the string
235 * passed in for the first non-digit character
236 * and end the string at that location. If we
237 * don't find any non-digit character we still
238 * need to place a '\0' at position nob since
239 * we are not interested in the rest of the
240 * string which is longer than nob in size.
241 * After we are done the character at the
242 * position we placed '\0' must be restored.
244 for (endp = str; endp < str + nob; endp++) {
245 if (!isdigit(*endp)) {
253 rc = kstrtouint(str, 10, num);
255 if (rc || !all_numbers)
258 return (*num >= min && *num <= max);
260 EXPORT_SYMBOL(cfs_str2num_check);
263 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
264 * \a src should only have a single token which can be \<number\> or \*
266 * \retval pointer to allocated range_expr and initialized
267 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
270 * \<number\> '-' \<number\> |
271 * \<number\> '-' \<number\> '/' \<number\>
272 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
273 * -ENOMEM will be returned.
276 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
277 int bracketed, struct cfs_range_expr **expr)
279 struct cfs_range_expr *re;
282 LIBCFS_ALLOC(re, sizeof(*re));
286 if (src->ls_len == 1 && src->ls_str[0] == '*') {
293 if (cfs_str2num_check(src->ls_str, src->ls_len,
294 &re->re_lo, min, max)) {
295 /* <number> is parsed */
296 re->re_hi = re->re_lo;
301 if (!bracketed || !cfs_gettok(src, '-', &tok))
304 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
305 &re->re_lo, min, max))
309 if (cfs_str2num_check(src->ls_str, src->ls_len,
310 &re->re_hi, min, max)) {
311 /* <number> - <number> is parsed */
316 /* go to check <number> '-' <number> '/' <number> */
317 if (cfs_gettok(src, '/', &tok)) {
318 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
319 &re->re_hi, min, max))
322 /* <number> - <number> / ... */
323 if (cfs_str2num_check(src->ls_str, src->ls_len,
324 &re->re_stride, min, max)) {
325 /* <number> - <number> / <number> is parsed */
335 LIBCFS_FREE(re, sizeof(*re));
340 * Print the range expression \a re into specified \a buffer.
341 * If \a bracketed is true, expression does not need additional
344 * \retval number of characters written
347 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
359 if (expr->re_lo == expr->re_hi)
360 i = scnprintf(buffer, count, "%u", expr->re_lo);
361 else if (expr->re_stride == 1)
362 i = scnprintf(buffer, count, "%s%u-%u%s",
363 s, expr->re_lo, expr->re_hi, e);
365 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
366 s, expr->re_lo, expr->re_hi, expr->re_stride, e);
371 * Print a list of range expressions (\a expr_list) into specified \a buffer.
372 * If the list contains several expressions, separate them with comma
373 * and surround the list with brackets.
375 * \retval number of characters written
378 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
380 struct cfs_range_expr *expr;
387 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
391 i += scnprintf(buffer + i, count - i, "[");
393 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
395 i += scnprintf(buffer + i, count - i, ",");
396 i += cfs_range_expr_print(buffer + i, count - i, expr,
401 i += scnprintf(buffer + i, count - i, "]");
405 EXPORT_SYMBOL(cfs_expr_list_print);
408 * Matches value (\a value) against ranges expression list \a expr_list.
410 * \retval 1 if \a value matches
411 * \retval 0 otherwise
414 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
416 struct cfs_range_expr *expr;
418 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
419 if (value >= expr->re_lo && value <= expr->re_hi &&
420 ((value - expr->re_lo) % expr->re_stride) == 0)
426 EXPORT_SYMBOL(cfs_expr_list_match);
429 * Convert express list (\a expr_list) to an array of all matched values
431 * \retval N N is total number of all matched values
432 * \retval 0 if expression list is empty
433 * \retval < 0 for failure
436 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
438 struct cfs_range_expr *expr;
443 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
444 for (i = expr->re_lo; i <= expr->re_hi; i++) {
445 if (((i - expr->re_lo) % expr->re_stride) == 0)
450 if (count == 0) /* empty expression list */
454 CERROR("Number of values %d exceeds max allowed %d\n",
459 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
464 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
465 for (i = expr->re_lo; i <= expr->re_hi; i++) {
466 if (((i - expr->re_lo) % expr->re_stride) == 0)
474 EXPORT_SYMBOL(cfs_expr_list_values);
477 * Frees cfs_range_expr structures of \a expr_list.
482 cfs_expr_list_free(struct cfs_expr_list *expr_list)
484 while (!list_empty(&expr_list->el_exprs)) {
485 struct cfs_range_expr *expr;
487 expr = list_entry(expr_list->el_exprs.next,
488 struct cfs_range_expr, re_link);
489 list_del(&expr->re_link);
490 LIBCFS_FREE(expr, sizeof(*expr));
493 LIBCFS_FREE(expr_list, sizeof(*expr_list));
495 EXPORT_SYMBOL(cfs_expr_list_free);
498 * Parses \<cfs_expr_list\> token of the syntax.
500 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
501 * \retval -errno otherwise
504 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
505 struct cfs_expr_list **elpp)
507 struct cfs_expr_list *expr_list;
508 struct cfs_range_expr *expr;
512 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
519 INIT_LIST_HEAD(&expr_list->el_exprs);
521 if (src.ls_str[0] == '[' &&
522 src.ls_str[src.ls_len - 1] == ']') {
530 if (!cfs_gettok(&src, ',', &tok)) {
535 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
539 list_add_tail(&expr->re_link, &expr_list->el_exprs);
542 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
544 list_add_tail(&expr->re_link, &expr_list->el_exprs);
548 cfs_expr_list_free(expr_list);
554 EXPORT_SYMBOL(cfs_expr_list_parse);
557 * Frees cfs_expr_list structures of \a list.
559 * For each struct cfs_expr_list structure found on \a list it frees
560 * range_expr list attached to it and frees the cfs_expr_list itself.
565 cfs_expr_list_free_list(struct list_head *list)
567 struct cfs_expr_list *el;
569 while (!list_empty(list)) {
570 el = list_entry(list->next, struct cfs_expr_list, el_link);
571 list_del(&el->el_link);
572 cfs_expr_list_free(el);
575 EXPORT_SYMBOL(cfs_expr_list_free_list);