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, 2015 Intel Corporation.
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 "../../include/linux/libcfs/libcfs.h"
45 /* Convert a text string to a bitmask */
46 int cfs_str2mask(const char *str
, const char *(*bit2str
)(int bit
),
47 int *oldmask
, int minmask
, int allmask
)
51 int newmask
= minmask
, i
, len
, found
= 0;
53 /* <str> must be a list of tokens separated by whitespace
54 * and optionally an operator ('+' or '-'). If an operator
55 * appears first in <str>, '*oldmask' is used as the starting point
56 * (relative), otherwise minmask is used (absolute). An operator
57 * applies to all following tokens up to the next operator.
59 while (*str
!= '\0') {
64 if (*str
== '+' || *str
== '-') {
67 /* only if first token is relative */
71 if (*str
== '\0') /* trailing op */
75 /* find token length */
77 while (str
[len
] != '\0' && !isspace(str
[len
]) &&
78 str
[len
] != '+' && str
[len
] != '-')
83 for (i
= 0; i
< 32; i
++) {
84 debugstr
= bit2str(i
);
85 if (debugstr
&& strlen(debugstr
) == len
&&
86 strncasecmp(str
, debugstr
, len
) == 0) {
95 if (!found
&& len
== 3 &&
96 (strncasecmp(str
, "ALL", len
) == 0)) {
104 CWARN("unknown mask '%.*s'.\n"
105 "mask usage: [+|-]<all|type> ...\n", len
, str
);
115 /* get the first string out of @str */
116 char *cfs_firststr(char *str
, size_t size
)
121 /* trim leading spaces */
122 while (i
< size
&& *str
&& isspace(*str
)) {
127 /* string with all spaces */
132 while (i
< size
&& *end
!= '\0' && !isspace(*end
)) {
141 EXPORT_SYMBOL(cfs_firststr
);
144 cfs_trimwhite(char *str
)
148 while (isspace(*str
))
151 end
= str
+ strlen(str
);
153 if (!isspace(end
[-1]))
161 EXPORT_SYMBOL(cfs_trimwhite
);
164 * Extracts tokens from strings.
166 * Looks for \a delim in string \a next, sets \a res to point to
167 * substring before the delimiter, sets \a next right after the found
170 * \retval 1 if \a res points to a string of non-whitespace characters
171 * \retval 0 otherwise
174 cfs_gettok(struct cfs_lstr
*next
, char delim
, struct cfs_lstr
*res
)
181 /* skip leading white spaces */
182 while (next
->ls_len
) {
183 if (!isspace(*next
->ls_str
))
189 if (next
->ls_len
== 0) /* whitespaces only */
192 if (*next
->ls_str
== delim
) {
193 /* first non-writespace is the delimiter */
197 res
->ls_str
= next
->ls_str
;
198 end
= memchr(next
->ls_str
, delim
, next
->ls_len
);
200 /* there is no the delimeter in the string */
201 end
= next
->ls_str
+ next
->ls_len
;
204 next
->ls_str
= end
+ 1;
205 next
->ls_len
-= (end
- res
->ls_str
+ 1);
208 /* skip ending whitespaces */
209 while (--end
!= res
->ls_str
) {
214 res
->ls_len
= end
- res
->ls_str
+ 1;
217 EXPORT_SYMBOL(cfs_gettok
);
220 * Converts string to integer.
222 * Accepts decimal and hexadecimal number recordings.
224 * \retval 1 if first \a nob chars of \a str convert to decimal or
225 * hexadecimal integer in the range [\a min, \a max]
226 * \retval 0 otherwise
229 cfs_str2num_check(char *str
, int nob
, unsigned *num
,
230 unsigned min
, unsigned max
)
234 str
= cfs_trimwhite(str
);
235 *num
= simple_strtoul(str
, &endp
, 0);
239 for (; endp
< str
+ nob
; endp
++) {
244 return (*num
>= min
&& *num
<= max
);
246 EXPORT_SYMBOL(cfs_str2num_check
);
249 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
250 * \a src should only have a single token which can be \<number\> or \*
252 * \retval pointer to allocated range_expr and initialized
253 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
256 * \<number\> '-' \<number\> |
257 * \<number\> '-' \<number\> '/' \<number\>
258 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
259 * -ENOMEM will be returned.
262 cfs_range_expr_parse(struct cfs_lstr
*src
, unsigned min
, unsigned max
,
263 int bracketed
, struct cfs_range_expr
**expr
)
265 struct cfs_range_expr
*re
;
268 LIBCFS_ALLOC(re
, sizeof(*re
));
272 if (src
->ls_len
== 1 && src
->ls_str
[0] == '*') {
279 if (cfs_str2num_check(src
->ls_str
, src
->ls_len
,
280 &re
->re_lo
, min
, max
)) {
281 /* <number> is parsed */
282 re
->re_hi
= re
->re_lo
;
287 if (!bracketed
|| !cfs_gettok(src
, '-', &tok
))
290 if (!cfs_str2num_check(tok
.ls_str
, tok
.ls_len
,
291 &re
->re_lo
, min
, max
))
295 if (cfs_str2num_check(src
->ls_str
, src
->ls_len
,
296 &re
->re_hi
, min
, max
)) {
297 /* <number> - <number> is parsed */
302 /* go to check <number> '-' <number> '/' <number> */
303 if (cfs_gettok(src
, '/', &tok
)) {
304 if (!cfs_str2num_check(tok
.ls_str
, tok
.ls_len
,
305 &re
->re_hi
, min
, max
))
308 /* <number> - <number> / ... */
309 if (cfs_str2num_check(src
->ls_str
, src
->ls_len
,
310 &re
->re_stride
, min
, max
)) {
311 /* <number> - <number> / <number> is parsed */
321 LIBCFS_FREE(re
, sizeof(*re
));
326 * Print the range expression \a re into specified \a buffer.
327 * If \a bracketed is true, expression does not need additional
330 * \retval number of characters written
333 cfs_range_expr_print(char *buffer
, int count
, struct cfs_range_expr
*expr
,
345 if (expr
->re_lo
== expr
->re_hi
)
346 i
= scnprintf(buffer
, count
, "%u", expr
->re_lo
);
347 else if (expr
->re_stride
== 1)
348 i
= scnprintf(buffer
, count
, "%s%u-%u%s",
349 s
, expr
->re_lo
, expr
->re_hi
, e
);
351 i
= scnprintf(buffer
, count
, "%s%u-%u/%u%s",
352 s
, expr
->re_lo
, expr
->re_hi
, expr
->re_stride
, e
);
357 * Print a list of range expressions (\a expr_list) into specified \a buffer.
358 * If the list contains several expressions, separate them with comma
359 * and surround the list with brackets.
361 * \retval number of characters written
364 cfs_expr_list_print(char *buffer
, int count
, struct cfs_expr_list
*expr_list
)
366 struct cfs_range_expr
*expr
;
373 list_for_each_entry(expr
, &expr_list
->el_exprs
, re_link
)
377 i
+= scnprintf(buffer
+ i
, count
- i
, "[");
379 list_for_each_entry(expr
, &expr_list
->el_exprs
, re_link
) {
381 i
+= scnprintf(buffer
+ i
, count
- i
, ",");
382 i
+= cfs_range_expr_print(buffer
+ i
, count
- i
, expr
,
387 i
+= scnprintf(buffer
+ i
, count
- i
, "]");
391 EXPORT_SYMBOL(cfs_expr_list_print
);
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 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 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 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 (!list_empty(&expr_list
->el_exprs
)) {
471 struct cfs_range_expr
*expr
;
473 expr
= list_entry(expr_list
->el_exprs
.next
,
474 struct cfs_range_expr
, re_link
);
475 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 * Parses \<cfs_expr_list\> token of the syntax.
486 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
487 * \retval -errno otherwise
490 cfs_expr_list_parse(char *str
, int len
, unsigned min
, unsigned max
,
491 struct cfs_expr_list
**elpp
)
493 struct cfs_expr_list
*expr_list
;
494 struct cfs_range_expr
*expr
;
498 LIBCFS_ALLOC(expr_list
, sizeof(*expr_list
));
505 INIT_LIST_HEAD(&expr_list
->el_exprs
);
507 if (src
.ls_str
[0] == '[' &&
508 src
.ls_str
[src
.ls_len
- 1] == ']') {
516 if (!cfs_gettok(&src
, ',', &tok
)) {
521 rc
= cfs_range_expr_parse(&tok
, min
, max
, 1, &expr
);
525 list_add_tail(&expr
->re_link
, &expr_list
->el_exprs
);
528 rc
= cfs_range_expr_parse(&src
, min
, max
, 0, &expr
);
530 list_add_tail(&expr
->re_link
, &expr_list
->el_exprs
);
534 cfs_expr_list_free(expr_list
);
540 EXPORT_SYMBOL(cfs_expr_list_parse
);
543 * Frees cfs_expr_list structures of \a list.
545 * For each struct cfs_expr_list structure found on \a list it frees
546 * range_expr list attached to it and frees the cfs_expr_list itself.
551 cfs_expr_list_free_list(struct list_head
*list
)
553 struct cfs_expr_list
*el
;
555 while (!list_empty(list
)) {
556 el
= list_entry(list
->next
, struct cfs_expr_list
, el_link
);
557 list_del(&el
->el_link
);
558 cfs_expr_list_free(el
);
561 EXPORT_SYMBOL(cfs_expr_list_free_list
);
This page took 0.051324 seconds and 6 git commands to generate.