Merge tag 'iio-for-4.6c' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio...
[deliverable/linux.git] / drivers / staging / lustre / lustre / libcfs / libcfs_string.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2012, 2015 Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * String manipulation functions.
37 *
38 * libcfs/libcfs/libcfs_string.c
39 *
40 * Author: Nathan Rutman <nathan.rutman@sun.com>
41 */
42
43 #include "../../include/linux/libcfs/libcfs.h"
44
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)
48 {
49 const char *debugstr;
50 char op = '\0';
51 int newmask = minmask, i, len, found = 0;
52
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.
58 */
59 while (*str != '\0') {
60 while (isspace(*str))
61 str++;
62 if (*str == '\0')
63 break;
64 if (*str == '+' || *str == '-') {
65 op = *str++;
66 if (!found)
67 /* only if first token is relative */
68 newmask = *oldmask;
69 while (isspace(*str))
70 str++;
71 if (*str == '\0') /* trailing op */
72 return -EINVAL;
73 }
74
75 /* find token length */
76 len = 0;
77 while (str[len] != '\0' && !isspace(str[len]) &&
78 str[len] != '+' && str[len] != '-')
79 len++;
80
81 /* match token */
82 found = 0;
83 for (i = 0; i < 32; i++) {
84 debugstr = bit2str(i);
85 if (debugstr && strlen(debugstr) == len &&
86 strncasecmp(str, debugstr, len) == 0) {
87 if (op == '-')
88 newmask &= ~(1 << i);
89 else
90 newmask |= (1 << i);
91 found = 1;
92 break;
93 }
94 }
95 if (!found && len == 3 &&
96 (strncasecmp(str, "ALL", len) == 0)) {
97 if (op == '-')
98 newmask = minmask;
99 else
100 newmask = allmask;
101 found = 1;
102 }
103 if (!found) {
104 CWARN("unknown mask '%.*s'.\n"
105 "mask usage: [+|-]<all|type> ...\n", len, str);
106 return -EINVAL;
107 }
108 str += len;
109 }
110
111 *oldmask = newmask;
112 return 0;
113 }
114
115 /* get the first string out of @str */
116 char *cfs_firststr(char *str, size_t size)
117 {
118 size_t i = 0;
119 char *end;
120
121 /* trim leading spaces */
122 while (i < size && *str && isspace(*str)) {
123 ++i;
124 ++str;
125 }
126
127 /* string with all spaces */
128 if (*str == '\0')
129 goto out;
130
131 end = str;
132 while (i < size && *end != '\0' && !isspace(*end)) {
133 ++i;
134 ++end;
135 }
136
137 *end = '\0';
138 out:
139 return str;
140 }
141 EXPORT_SYMBOL(cfs_firststr);
142
143 char *
144 cfs_trimwhite(char *str)
145 {
146 char *end;
147
148 while (isspace(*str))
149 str++;
150
151 end = str + strlen(str);
152 while (end > str) {
153 if (!isspace(end[-1]))
154 break;
155 end--;
156 }
157
158 *end = 0;
159 return str;
160 }
161 EXPORT_SYMBOL(cfs_trimwhite);
162
163 /**
164 * Extracts tokens from strings.
165 *
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
168 * delimiter.
169 *
170 * \retval 1 if \a res points to a string of non-whitespace characters
171 * \retval 0 otherwise
172 */
173 int
174 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
175 {
176 char *end;
177
178 if (!next->ls_str)
179 return 0;
180
181 /* skip leading white spaces */
182 while (next->ls_len) {
183 if (!isspace(*next->ls_str))
184 break;
185 next->ls_str++;
186 next->ls_len--;
187 }
188
189 if (next->ls_len == 0) /* whitespaces only */
190 return 0;
191
192 if (*next->ls_str == delim) {
193 /* first non-writespace is the delimiter */
194 return 0;
195 }
196
197 res->ls_str = next->ls_str;
198 end = memchr(next->ls_str, delim, next->ls_len);
199 if (!end) {
200 /* there is no the delimeter in the string */
201 end = next->ls_str + next->ls_len;
202 next->ls_str = NULL;
203 } else {
204 next->ls_str = end + 1;
205 next->ls_len -= (end - res->ls_str + 1);
206 }
207
208 /* skip ending whitespaces */
209 while (--end != res->ls_str) {
210 if (!isspace(*end))
211 break;
212 }
213
214 res->ls_len = end - res->ls_str + 1;
215 return 1;
216 }
217 EXPORT_SYMBOL(cfs_gettok);
218
219 /**
220 * Converts string to integer.
221 *
222 * Accepts decimal and hexadecimal number recordings.
223 *
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
227 */
228 int
229 cfs_str2num_check(char *str, int nob, unsigned *num,
230 unsigned min, unsigned max)
231 {
232 int rc;
233
234 str = cfs_trimwhite(str);
235 rc = kstrtouint(str, 10, num);
236 if (rc)
237 return 0;
238
239 return (*num >= min && *num <= max);
240 }
241 EXPORT_SYMBOL(cfs_str2num_check);
242
243 /**
244 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
245 * \a src should only have a single token which can be \<number\> or \*
246 *
247 * \retval pointer to allocated range_expr and initialized
248 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
249 `* src parses to
250 * \<number\> |
251 * \<number\> '-' \<number\> |
252 * \<number\> '-' \<number\> '/' \<number\>
253 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
254 * -ENOMEM will be returned.
255 */
256 static int
257 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
258 int bracketed, struct cfs_range_expr **expr)
259 {
260 struct cfs_range_expr *re;
261 struct cfs_lstr tok;
262
263 LIBCFS_ALLOC(re, sizeof(*re));
264 if (!re)
265 return -ENOMEM;
266
267 if (src->ls_len == 1 && src->ls_str[0] == '*') {
268 re->re_lo = min;
269 re->re_hi = max;
270 re->re_stride = 1;
271 goto out;
272 }
273
274 if (cfs_str2num_check(src->ls_str, src->ls_len,
275 &re->re_lo, min, max)) {
276 /* <number> is parsed */
277 re->re_hi = re->re_lo;
278 re->re_stride = 1;
279 goto out;
280 }
281
282 if (!bracketed || !cfs_gettok(src, '-', &tok))
283 goto failed;
284
285 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
286 &re->re_lo, min, max))
287 goto failed;
288
289 /* <number> - */
290 if (cfs_str2num_check(src->ls_str, src->ls_len,
291 &re->re_hi, min, max)) {
292 /* <number> - <number> is parsed */
293 re->re_stride = 1;
294 goto out;
295 }
296
297 /* go to check <number> '-' <number> '/' <number> */
298 if (cfs_gettok(src, '/', &tok)) {
299 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
300 &re->re_hi, min, max))
301 goto failed;
302
303 /* <number> - <number> / ... */
304 if (cfs_str2num_check(src->ls_str, src->ls_len,
305 &re->re_stride, min, max)) {
306 /* <number> - <number> / <number> is parsed */
307 goto out;
308 }
309 }
310
311 out:
312 *expr = re;
313 return 0;
314
315 failed:
316 LIBCFS_FREE(re, sizeof(*re));
317 return -EINVAL;
318 }
319
320 /**
321 * Print the range expression \a re into specified \a buffer.
322 * If \a bracketed is true, expression does not need additional
323 * brackets.
324 *
325 * \retval number of characters written
326 */
327 static int
328 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
329 bool bracketed)
330 {
331 int i;
332 char s[] = "[";
333 char e[] = "]";
334
335 if (bracketed) {
336 s[0] = '\0';
337 e[0] = '\0';
338 }
339
340 if (expr->re_lo == expr->re_hi)
341 i = scnprintf(buffer, count, "%u", expr->re_lo);
342 else if (expr->re_stride == 1)
343 i = scnprintf(buffer, count, "%s%u-%u%s",
344 s, expr->re_lo, expr->re_hi, e);
345 else
346 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
347 s, expr->re_lo, expr->re_hi, expr->re_stride, e);
348 return i;
349 }
350
351 /**
352 * Print a list of range expressions (\a expr_list) into specified \a buffer.
353 * If the list contains several expressions, separate them with comma
354 * and surround the list with brackets.
355 *
356 * \retval number of characters written
357 */
358 int
359 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
360 {
361 struct cfs_range_expr *expr;
362 int i = 0, j = 0;
363 int numexprs = 0;
364
365 if (count <= 0)
366 return 0;
367
368 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
369 numexprs++;
370
371 if (numexprs > 1)
372 i += scnprintf(buffer + i, count - i, "[");
373
374 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
375 if (j++ != 0)
376 i += scnprintf(buffer + i, count - i, ",");
377 i += cfs_range_expr_print(buffer + i, count - i, expr,
378 numexprs > 1);
379 }
380
381 if (numexprs > 1)
382 i += scnprintf(buffer + i, count - i, "]");
383
384 return i;
385 }
386 EXPORT_SYMBOL(cfs_expr_list_print);
387
388 /**
389 * Matches value (\a value) against ranges expression list \a expr_list.
390 *
391 * \retval 1 if \a value matches
392 * \retval 0 otherwise
393 */
394 int
395 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
396 {
397 struct cfs_range_expr *expr;
398
399 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
400 if (value >= expr->re_lo && value <= expr->re_hi &&
401 ((value - expr->re_lo) % expr->re_stride) == 0)
402 return 1;
403 }
404
405 return 0;
406 }
407 EXPORT_SYMBOL(cfs_expr_list_match);
408
409 /**
410 * Convert express list (\a expr_list) to an array of all matched values
411 *
412 * \retval N N is total number of all matched values
413 * \retval 0 if expression list is empty
414 * \retval < 0 for failure
415 */
416 int
417 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
418 {
419 struct cfs_range_expr *expr;
420 __u32 *val;
421 int count = 0;
422 int i;
423
424 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
425 for (i = expr->re_lo; i <= expr->re_hi; i++) {
426 if (((i - expr->re_lo) % expr->re_stride) == 0)
427 count++;
428 }
429 }
430
431 if (count == 0) /* empty expression list */
432 return 0;
433
434 if (count > max) {
435 CERROR("Number of values %d exceeds max allowed %d\n",
436 max, count);
437 return -EINVAL;
438 }
439
440 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
441 if (!val)
442 return -ENOMEM;
443
444 count = 0;
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)
448 val[count++] = i;
449 }
450 }
451
452 *valpp = val;
453 return count;
454 }
455 EXPORT_SYMBOL(cfs_expr_list_values);
456
457 /**
458 * Frees cfs_range_expr structures of \a expr_list.
459 *
460 * \retval none
461 */
462 void
463 cfs_expr_list_free(struct cfs_expr_list *expr_list)
464 {
465 while (!list_empty(&expr_list->el_exprs)) {
466 struct cfs_range_expr *expr;
467
468 expr = list_entry(expr_list->el_exprs.next,
469 struct cfs_range_expr, re_link);
470 list_del(&expr->re_link);
471 LIBCFS_FREE(expr, sizeof(*expr));
472 }
473
474 LIBCFS_FREE(expr_list, sizeof(*expr_list));
475 }
476 EXPORT_SYMBOL(cfs_expr_list_free);
477
478 /**
479 * Parses \<cfs_expr_list\> token of the syntax.
480 *
481 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
482 * \retval -errno otherwise
483 */
484 int
485 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
486 struct cfs_expr_list **elpp)
487 {
488 struct cfs_expr_list *expr_list;
489 struct cfs_range_expr *expr;
490 struct cfs_lstr src;
491 int rc;
492
493 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
494 if (!expr_list)
495 return -ENOMEM;
496
497 src.ls_str = str;
498 src.ls_len = len;
499
500 INIT_LIST_HEAD(&expr_list->el_exprs);
501
502 if (src.ls_str[0] == '[' &&
503 src.ls_str[src.ls_len - 1] == ']') {
504 src.ls_str++;
505 src.ls_len -= 2;
506
507 rc = -EINVAL;
508 while (src.ls_str) {
509 struct cfs_lstr tok;
510
511 if (!cfs_gettok(&src, ',', &tok)) {
512 rc = -EINVAL;
513 break;
514 }
515
516 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
517 if (rc != 0)
518 break;
519
520 list_add_tail(&expr->re_link, &expr_list->el_exprs);
521 }
522 } else {
523 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
524 if (rc == 0)
525 list_add_tail(&expr->re_link, &expr_list->el_exprs);
526 }
527
528 if (rc != 0)
529 cfs_expr_list_free(expr_list);
530 else
531 *elpp = expr_list;
532
533 return rc;
534 }
535 EXPORT_SYMBOL(cfs_expr_list_parse);
536
537 /**
538 * Frees cfs_expr_list structures of \a list.
539 *
540 * For each struct cfs_expr_list structure found on \a list it frees
541 * range_expr list attached to it and frees the cfs_expr_list itself.
542 *
543 * \retval none
544 */
545 void
546 cfs_expr_list_free_list(struct list_head *list)
547 {
548 struct cfs_expr_list *el;
549
550 while (!list_empty(list)) {
551 el = list_entry(list->next, struct cfs_expr_list, el_link);
552 list_del(&el->el_link);
553 cfs_expr_list_free(el);
554 }
555 }
556 EXPORT_SYMBOL(cfs_expr_list_free_list);
This page took 0.048642 seconds and 6 git commands to generate.