2 * lib/parser.c - simple parser for mount, etc. options.
4 * This source code is licensed under the GNU General Public License,
5 * Version 2. See the file COPYING for more details.
8 #include <linux/ctype.h>
9 #include <linux/module.h>
10 #include <linux/parser.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
14 static int match_one(char *s, char *p, substring_t args[])
24 meta = strchr(p, '%');
26 return strcmp(p, s) == 0;
28 if (strncmp(p, s, meta-p))
35 len = simple_strtoul(p, &p, 10);
42 if (argc >= MAX_OPT_ARGS)
48 if (len == -1 || len > strlen(s))
50 args[argc].to = s + len;
53 simple_strtol(s, &args[argc].to, 0);
56 simple_strtoul(s, &args[argc].to, 0);
59 simple_strtoul(s, &args[argc].to, 8);
62 simple_strtoul(s, &args[argc].to, 16);
64 if (args[argc].to == args[argc].from)
75 int match_token(char *s, match_table_t table, substring_t args[])
77 struct match_token *p;
79 for (p = table; !match_one(s, p->pattern, args) ; p++)
85 static int match_number(substring_t *s, int *result, int base)
91 buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
94 memcpy(buf, s->from, s->to - s->from);
95 buf[s->to - s->from] = '\0';
96 *result = simple_strtol(buf, &endp, base);
104 int match_int(substring_t *s, int *result)
106 return match_number(s, result, 0);
109 int match_octal(substring_t *s, int *result)
111 return match_number(s, result, 8);
114 int match_hex(substring_t *s, int *result)
116 return match_number(s, result, 16);
119 void match_strcpy(char *to, substring_t *s)
121 memcpy(to, s->from, s->to - s->from);
122 to[s->to - s->from] = '\0';
125 char *match_strdup(substring_t *s)
127 char *p = kmalloc(s->to - s->from + 1, GFP_KERNEL);
133 EXPORT_SYMBOL(match_token);
134 EXPORT_SYMBOL(match_int);
135 EXPORT_SYMBOL(match_octal);
136 EXPORT_SYMBOL(match_hex);
137 EXPORT_SYMBOL(match_strcpy);
138 EXPORT_SYMBOL(match_strdup);