Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / libcfs / 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, Whamcloud, Inc.
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 <libcfs/libcfs.h>
44
45 /* non-0 = don't match */
46 static int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
47 {
48         if (s1 == NULL || s2 == NULL)
49                 return 1;
50
51         if (n == 0)
52                 return 0;
53
54         while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
55                 if (n == 0 || *s1 == '\0' || *s2 == '\0')
56                         break;
57                 s1++;
58                 s2++;
59         }
60
61         return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
62 }
63
64 /* Convert a text string to a bitmask */
65 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
66                  int *oldmask, int minmask, int allmask)
67 {
68         const char *debugstr;
69         char op = 0;
70         int newmask = minmask, i, len, found = 0;
71         ENTRY;
72
73         /* <str> must be a list of tokens separated by whitespace
74          * and optionally an operator ('+' or '-').  If an operator
75          * appears first in <str>, '*oldmask' is used as the starting point
76          * (relative), otherwise minmask is used (absolute).  An operator
77          * applies to all following tokens up to the next operator. */
78         while (*str != 0) {
79                 while (isspace(*str))
80                         str++;
81                 if (*str == 0)
82                         break;
83                 if (*str == '+' || *str == '-') {
84                         op = *str++;
85                         if (!found)
86                                 /* only if first token is relative */
87                                 newmask = *oldmask;
88                         while (isspace(*str))
89                                 str++;
90                         if (*str == 0)          /* trailing op */
91                                 return -EINVAL;
92                 }
93
94                 /* find token length */
95                 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
96                       str[len] != '+' && str[len] != '-'; len++);
97
98                 /* match token */
99                 found = 0;
100                 for (i = 0; i < 32; i++) {
101                         debugstr = bit2str(i);
102                         if (debugstr != NULL &&
103                             strlen(debugstr) == len &&
104                             cfs_strncasecmp(str, debugstr, len) == 0) {
105                                 if (op == '-')
106                                         newmask &= ~(1 << i);
107                                 else
108                                         newmask |= (1 << i);
109                                 found = 1;
110                                 break;
111                         }
112                 }
113                 if (!found && len == 3 &&
114                     (cfs_strncasecmp(str, "ALL", len) == 0)) {
115                         if (op == '-')
116                                 newmask = minmask;
117                         else
118                                 newmask = allmask;
119                         found = 1;
120                 }
121                 if (!found) {
122                         CWARN("unknown mask '%.*s'.\n"
123                               "mask usage: [+|-]<all|type> ...\n", len, str);
124                         return -EINVAL;
125                 }
126                 str += len;
127         }
128
129         *oldmask = newmask;
130         return 0;
131 }
132 EXPORT_SYMBOL(cfs_str2mask);
133
134 /* Duplicate a string in a platform-independent way */
135 char *cfs_strdup(const char *str, u_int32_t flags)
136 {
137         size_t lenz; /* length of str + zero byte */
138         char *dup_str;
139
140         lenz = strlen(str) + 1;
141
142         dup_str = cfs_alloc(lenz, flags);
143         if (dup_str == NULL)
144                 return NULL;
145
146         memcpy(dup_str, str, lenz);
147
148         return dup_str;
149 }
150 EXPORT_SYMBOL(cfs_strdup);
151
152 /**
153  * cfs_{v}snprintf() return the actual size that is printed rather than
154  * the size that would be printed in standard functions.
155  */
156 /* safe vsnprintf */
157 int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
158 {
159         int i;
160
161         LASSERT(size > 0);
162         i = vsnprintf(buf, size, fmt, args);
163
164         return  (i >= size ? size - 1 : i);
165 }
166 EXPORT_SYMBOL(cfs_vsnprintf);
167
168 /* safe snprintf */
169 int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
170 {
171         va_list args;
172         int i;
173
174         va_start(args, fmt);
175         i = cfs_vsnprintf(buf, size, fmt, args);
176         va_end(args);
177
178         return  i;
179 }
180 EXPORT_SYMBOL(cfs_snprintf);
181
182 /* get the first string out of @str */
183 char *cfs_firststr(char *str, size_t size)
184 {
185         size_t i = 0;
186         char  *end;
187
188         /* trim leading spaces */
189         while (i < size && *str && isspace(*str)) {
190                 ++i;
191                 ++str;
192         }
193
194         /* string with all spaces */
195         if (*str == '\0')
196                 goto out;
197
198         end = str;
199         while (i < size && *end != '\0' && !isspace(*end)) {
200                 ++i;
201                 ++end;
202         }
203
204         *end= '\0';
205 out:
206         return str;
207 }
208 EXPORT_SYMBOL(cfs_firststr);
209
210 char *
211 cfs_trimwhite(char *str)
212 {
213         char *end;
214
215         while (cfs_iswhite(*str))
216                 str++;
217
218         end = str + strlen(str);
219         while (end > str) {
220                 if (!cfs_iswhite(end[-1]))
221                         break;
222                 end--;
223         }
224
225         *end = 0;
226         return str;
227 }
228 EXPORT_SYMBOL(cfs_trimwhite);
229
230 /**
231  * Extracts tokens from strings.
232  *
233  * Looks for \a delim in string \a next, sets \a res to point to
234  * substring before the delimiter, sets \a next right after the found
235  * delimiter.
236  *
237  * \retval 1 if \a res points to a string of non-whitespace characters
238  * \retval 0 otherwise
239  */
240 int
241 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
242 {
243         char *end;
244
245         if (next->ls_str == NULL)
246                 return 0;
247
248         /* skip leading white spaces */
249         while (next->ls_len) {
250                 if (!cfs_iswhite(*next->ls_str))
251                         break;
252                 next->ls_str++;
253                 next->ls_len--;
254         }
255
256         if (next->ls_len == 0) /* whitespaces only */
257                 return 0;
258
259         if (*next->ls_str == delim) {
260                 /* first non-writespace is the delimiter */
261                 return 0;
262         }
263
264         res->ls_str = next->ls_str;
265         end = memchr(next->ls_str, delim, next->ls_len);
266         if (end == NULL) {
267                 /* there is no the delimeter in the string */
268                 end = next->ls_str + next->ls_len;
269                 next->ls_str = NULL;
270         } else {
271                 next->ls_str = end + 1;
272                 next->ls_len -= (end - res->ls_str + 1);
273         }
274
275         /* skip ending whitespaces */
276         while (--end != res->ls_str) {
277                 if (!cfs_iswhite(*end))
278                         break;
279         }
280
281         res->ls_len = end - res->ls_str + 1;
282         return 1;
283 }
284 EXPORT_SYMBOL(cfs_gettok);
285
286 /**
287  * Converts string to integer.
288  *
289  * Accepts decimal and hexadecimal number recordings.
290  *
291  * \retval 1 if first \a nob chars of \a str convert to decimal or
292  * hexadecimal integer in the range [\a min, \a max]
293  * \retval 0 otherwise
294  */
295 int
296 cfs_str2num_check(char *str, int nob, unsigned *num,
297                   unsigned min, unsigned max)
298 {
299         char    *endp;
300
301         str = cfs_trimwhite(str);
302         *num = strtoul(str, &endp, 0);
303         if (endp == str)
304                 return 0;
305
306         for (; endp < str + nob; endp++) {
307                 if (!cfs_iswhite(*endp))
308                         return 0;
309         }
310
311         return (*num >= min && *num <= max);
312 }
313 EXPORT_SYMBOL(cfs_str2num_check);
314
315 /**
316  * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
317  * \a src should only have a single token which can be \<number\> or  \*
318  *
319  * \retval pointer to allocated range_expr and initialized
320  * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
321  `* src parses to
322  * \<number\> |
323  * \<number\> '-' \<number\> |
324  * \<number\> '-' \<number\> '/' \<number\>
325  * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
326  * -ENOMEM will be returned.
327  */
328 int
329 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
330                      int bracketed, struct cfs_range_expr **expr)
331 {
332         struct cfs_range_expr   *re;
333         struct cfs_lstr         tok;
334
335         LIBCFS_ALLOC(re, sizeof(*re));
336         if (re == NULL)
337                 return -ENOMEM;
338
339         if (src->ls_len == 1 && src->ls_str[0] == '*') {
340                 re->re_lo = min;
341                 re->re_hi = max;
342                 re->re_stride = 1;
343                 goto out;
344         }
345
346         if (cfs_str2num_check(src->ls_str, src->ls_len,
347                               &re->re_lo, min, max)) {
348                 /* <number> is parsed */
349                 re->re_hi = re->re_lo;
350                 re->re_stride = 1;
351                 goto out;
352         }
353
354         if (!bracketed || !cfs_gettok(src, '-', &tok))
355                 goto failed;
356
357         if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
358                                &re->re_lo, min, max))
359                 goto failed;
360
361         /* <number> - */
362         if (cfs_str2num_check(src->ls_str, src->ls_len,
363                               &re->re_hi, min, max)) {
364                 /* <number> - <number> is parsed */
365                 re->re_stride = 1;
366                 goto out;
367         }
368
369         /* go to check <number> '-' <number> '/' <number> */
370         if (cfs_gettok(src, '/', &tok)) {
371                 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
372                                        &re->re_hi, min, max))
373                         goto failed;
374
375                 /* <number> - <number> / ... */
376                 if (cfs_str2num_check(src->ls_str, src->ls_len,
377                                       &re->re_stride, min, max)) {
378                         /* <number> - <number> / <number> is parsed */
379                         goto out;
380                 }
381         }
382
383  out:
384         *expr = re;
385         return 0;
386
387  failed:
388         LIBCFS_FREE(re, sizeof(*re));
389         return -EINVAL;
390 }
391 EXPORT_SYMBOL(cfs_range_expr_parse);
392
393 /**
394  * Matches value (\a value) against ranges expression list \a expr_list.
395  *
396  * \retval 1 if \a value matches
397  * \retval 0 otherwise
398  */
399 int
400 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
401 {
402         struct cfs_range_expr   *expr;
403
404         cfs_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)
407                         return 1;
408         }
409
410         return 0;
411 }
412 EXPORT_SYMBOL(cfs_expr_list_match);
413
414 /**
415  * Convert express list (\a expr_list) to an array of all matched values
416  *
417  * \retval N N is total number of all matched values
418  * \retval 0 if expression list is empty
419  * \retval < 0 for failure
420  */
421 int
422 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
423 {
424         struct cfs_range_expr   *expr;
425         __u32                   *val;
426         int                     count = 0;
427         int                     i;
428
429         cfs_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)
432                                 count++;
433                 }
434         }
435
436         if (count == 0) /* empty expression list */
437                 return 0;
438
439         if (count > max) {
440                 CERROR("Number of values %d exceeds max allowed %d\n",
441                        max, count);
442                 return -EINVAL;
443         }
444
445         LIBCFS_ALLOC(val, sizeof(val[0]) * count);
446         if (val == NULL)
447                 return -ENOMEM;
448
449         count = 0;
450         cfs_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)
453                                 val[count++] = i;
454                 }
455         }
456
457         *valpp = val;
458         return count;
459 }
460 EXPORT_SYMBOL(cfs_expr_list_values);
461
462 /**
463  * Frees cfs_range_expr structures of \a expr_list.
464  *
465  * \retval none
466  */
467 void
468 cfs_expr_list_free(struct cfs_expr_list *expr_list)
469 {
470         while (!cfs_list_empty(&expr_list->el_exprs)) {
471                 struct cfs_range_expr *expr;
472
473                 expr = cfs_list_entry(expr_list->el_exprs.next,
474                                       struct cfs_range_expr, re_link),
475                 cfs_list_del(&expr->re_link);
476                 LIBCFS_FREE(expr, sizeof(*expr));
477         }
478
479         LIBCFS_FREE(expr_list, sizeof(*expr_list));
480 }
481 EXPORT_SYMBOL(cfs_expr_list_free);
482
483 void
484 cfs_expr_list_print(struct cfs_expr_list *expr_list)
485 {
486         struct cfs_range_expr *expr;
487
488         cfs_list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
489                 CDEBUG(D_WARNING, "%d-%d/%d\n",
490                        expr->re_lo, expr->re_hi, expr->re_stride);
491         }
492 }
493 EXPORT_SYMBOL(cfs_expr_list_print);
494
495 /**
496  * Parses \<cfs_expr_list\> token of the syntax.
497  *
498  * \retval 1 if \a str parses to \<number\> | \<expr_list\>
499  * \retval 0 otherwise
500  */
501 int
502 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
503                     struct cfs_expr_list **elpp)
504 {
505         struct cfs_expr_list    *expr_list;
506         struct cfs_range_expr   *expr;
507         struct cfs_lstr         src;
508         int                     rc;
509
510         LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
511         if (expr_list == NULL)
512                 return -ENOMEM;
513
514         src.ls_str = str;
515         src.ls_len = len;
516
517         CFS_INIT_LIST_HEAD(&expr_list->el_exprs);
518
519         if (src.ls_str[0] == '[' &&
520             src.ls_str[src.ls_len - 1] == ']') {
521                 src.ls_str++;
522                 src.ls_len -= 2;
523
524                 rc = -EINVAL;
525                 while (src.ls_str != NULL) {
526                         struct cfs_lstr tok;
527
528                         if (!cfs_gettok(&src, ',', &tok)) {
529                                 rc = -EINVAL;
530                                 break;
531                         }
532
533                         rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
534                         if (rc != 0)
535                                 break;
536
537                         cfs_list_add_tail(&expr->re_link,
538                                           &expr_list->el_exprs);
539                 }
540         } else {
541                 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
542                 if (rc == 0) {
543                         cfs_list_add_tail(&expr->re_link,
544                                           &expr_list->el_exprs);
545                 }
546         }
547
548         if (rc != 0)
549                 cfs_expr_list_free(expr_list);
550         else
551                 *elpp = expr_list;
552
553         return rc;
554 }
555 EXPORT_SYMBOL(cfs_expr_list_parse);
556
557 /**
558  * Frees cfs_expr_list structures of \a list.
559  *
560  * For each struct cfs_expr_list structure found on \a list it frees
561  * range_expr list attached to it and frees the cfs_expr_list itself.
562  *
563  * \retval none
564  */
565 void
566 cfs_expr_list_free_list(cfs_list_t *list)
567 {
568         struct cfs_expr_list *el;
569
570         while (!cfs_list_empty(list)) {
571                 el = cfs_list_entry(list->next,
572                                     struct cfs_expr_list, el_link);
573                 cfs_list_del(&el->el_link);
574                 cfs_expr_list_free(el);
575         }
576 }
577 EXPORT_SYMBOL(cfs_expr_list_free_list);
578
579 int
580 cfs_ip_addr_parse(char *str, int len, cfs_list_t *list)
581 {
582         struct cfs_expr_list    *el;
583         struct cfs_lstr         src;
584         int                     rc;
585         int                     i;
586
587         src.ls_str = str;
588         src.ls_len = len;
589         i = 0;
590
591         while (src.ls_str != NULL) {
592                 struct cfs_lstr res;
593
594                 if (!cfs_gettok(&src, '.', &res)) {
595                         rc = -EINVAL;
596                         goto out;
597                 }
598
599                 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
600                 if (rc != 0)
601                         goto out;
602
603                 cfs_list_add_tail(&el->el_link, list);
604                 i++;
605         }
606
607         if (i == 4)
608                 return 0;
609
610         rc = -EINVAL;
611  out:
612         cfs_expr_list_free_list(list);
613
614         return rc;
615 }
616 EXPORT_SYMBOL(cfs_ip_addr_parse);
617
618 /**
619  * Matches address (\a addr) against address set encoded in \a list.
620  *
621  * \retval 1 if \a addr matches
622  * \retval 0 otherwise
623  */
624 int
625 cfs_ip_addr_match(__u32 addr, cfs_list_t *list)
626 {
627         struct cfs_expr_list *el;
628         int i = 0;
629
630         cfs_list_for_each_entry_reverse(el, list, el_link) {
631                 if (!cfs_expr_list_match(addr & 0xff, el))
632                         return 0;
633                 addr >>= 8;
634                 i++;
635         }
636
637         return i == 4;
638 }
639 EXPORT_SYMBOL(cfs_ip_addr_match);
640
641 void
642 cfs_ip_addr_free(cfs_list_t *list)
643 {
644         cfs_expr_list_free_list(list);
645 }
646 EXPORT_SYMBOL(cfs_ip_addr_free);