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;
232 str = cfs_trimwhite(str);
235 * kstrouint can only handle strings composed
236 * of only numbers. We need to scan the string
237 * passed in for the first non-digit character
238 * and end the string at that location. If we
239 * don't find any non-digit character we still
240 * need to place a '\0' at position nob since
241 * we are not interested in the rest of the
242 * string which is longer than nob in size.
243 * After we are done the character at the
244 * position we placed '\0' must be restored.
246 for (endp = str; endp < str + nob; endp++) {
247 if (!isdigit(*endp)) {
255 rc = kstrtouint(str, 10, num);
257 if (rc || !all_numbers)
260 return (*num >= min && *num <= max);
262 EXPORT_SYMBOL(cfs_str2num_check);
265 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
266 * \a src should only have a single token which can be \<number\> or \*
268 * \retval pointer to allocated range_expr and initialized
269 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
272 * \<number\> '-' \<number\> |
273 * \<number\> '-' \<number\> '/' \<number\>
274 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
275 * -ENOMEM will be returned.
278 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
279 int bracketed, struct cfs_range_expr **expr)
281 struct cfs_range_expr *re;
284 LIBCFS_ALLOC(re, sizeof(*re));
288 if (src->ls_len == 1 && src->ls_str[0] == '*') {
295 if (cfs_str2num_check(src->ls_str, src->ls_len,
296 &re->re_lo, min, max)) {
297 /* <number> is parsed */
298 re->re_hi = re->re_lo;
303 if (!bracketed || !cfs_gettok(src, '-', &tok))
306 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
307 &re->re_lo, min, max))
311 if (cfs_str2num_check(src->ls_str, src->ls_len,
312 &re->re_hi, min, max)) {
313 /* <number> - <number> is parsed */
318 /* go to check <number> '-' <number> '/' <number> */
319 if (cfs_gettok(src, '/', &tok)) {
320 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
321 &re->re_hi, min, max))
324 /* <number> - <number> / ... */
325 if (cfs_str2num_check(src->ls_str, src->ls_len,
326 &re->re_stride, min, max)) {
327 /* <number> - <number> / <number> is parsed */
337 LIBCFS_FREE(re, sizeof(*re));
342 * Print the range expression \a re into specified \a buffer.
343 * If \a bracketed is true, expression does not need additional
346 * \retval number of characters written
349 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
361 if (expr->re_lo == expr->re_hi)
362 i = scnprintf(buffer, count, "%u", expr->re_lo);
363 else if (expr->re_stride == 1)
364 i = scnprintf(buffer, count, "%s%u-%u%s",
365 s, expr->re_lo, expr->re_hi, e);
367 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
368 s, expr->re_lo, expr->re_hi, expr->re_stride, e);
373 * Print a list of range expressions (\a expr_list) into specified \a buffer.
374 * If the list contains several expressions, separate them with comma
375 * and surround the list with brackets.
377 * \retval number of characters written
380 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
382 struct cfs_range_expr *expr;
389 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
393 i += scnprintf(buffer + i, count - i, "[");
395 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
397 i += scnprintf(buffer + i, count - i, ",");
398 i += cfs_range_expr_print(buffer + i, count - i, expr,
403 i += scnprintf(buffer + i, count - i, "]");
407 EXPORT_SYMBOL(cfs_expr_list_print);
410 * Matches value (\a value) against ranges expression list \a expr_list.
412 * \retval 1 if \a value matches
413 * \retval 0 otherwise
416 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
418 struct cfs_range_expr *expr;
420 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
421 if (value >= expr->re_lo && value <= expr->re_hi &&
422 ((value - expr->re_lo) % expr->re_stride) == 0)
428 EXPORT_SYMBOL(cfs_expr_list_match);
431 * Convert express list (\a expr_list) to an array of all matched values
433 * \retval N N is total number of all matched values
434 * \retval 0 if expression list is empty
435 * \retval < 0 for failure
438 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
440 struct cfs_range_expr *expr;
445 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
446 for (i = expr->re_lo; i <= expr->re_hi; i++) {
447 if (((i - expr->re_lo) % expr->re_stride) == 0)
452 if (count == 0) /* empty expression list */
456 CERROR("Number of values %d exceeds max allowed %d\n",
461 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
466 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
467 for (i = expr->re_lo; i <= expr->re_hi; i++) {
468 if (((i - expr->re_lo) % expr->re_stride) == 0)
476 EXPORT_SYMBOL(cfs_expr_list_values);
479 * Frees cfs_range_expr structures of \a expr_list.
484 cfs_expr_list_free(struct cfs_expr_list *expr_list)
486 while (!list_empty(&expr_list->el_exprs)) {
487 struct cfs_range_expr *expr;
489 expr = list_entry(expr_list->el_exprs.next,
490 struct cfs_range_expr, re_link);
491 list_del(&expr->re_link);
492 LIBCFS_FREE(expr, sizeof(*expr));
495 LIBCFS_FREE(expr_list, sizeof(*expr_list));
497 EXPORT_SYMBOL(cfs_expr_list_free);
500 * Parses \<cfs_expr_list\> token of the syntax.
502 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
503 * \retval -errno otherwise
506 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
507 struct cfs_expr_list **elpp)
509 struct cfs_expr_list *expr_list;
510 struct cfs_range_expr *expr;
514 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
521 INIT_LIST_HEAD(&expr_list->el_exprs);
523 if (src.ls_str[0] == '[' &&
524 src.ls_str[src.ls_len - 1] == ']') {
532 if (!cfs_gettok(&src, ',', &tok)) {
537 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
541 list_add_tail(&expr->re_link, &expr_list->el_exprs);
544 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
546 list_add_tail(&expr->re_link, &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(struct list_head *list)
569 struct cfs_expr_list *el;
571 while (!list_empty(list)) {
572 el = list_entry(list->next, struct cfs_expr_list, el_link);
573 list_del(&el->el_link);
574 cfs_expr_list_free(el);
577 EXPORT_SYMBOL(cfs_expr_list_free_list);