Blame view

lib/parser.c 8.37 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  /*
   * lib/parser.c - simple parser for mount, etc. options.
   *
   * This source code is licensed under the GNU General Public License,
   * Version 2.  See the file COPYING for more details.
   */
  
  #include <linux/ctype.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
9
10
  #include <linux/types.h>
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
  #include <linux/parser.h>
  #include <linux/slab.h>
  #include <linux/string.h>
  
  /**
   * match_one: - Determines if a string matches a simple pattern
25985edce   Lucas De Marchi   Fix common misspe...
17
   * @s: the string to examine for presence of the pattern
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
25
   * @p: the string containing the pattern
   * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
   * locations.
   *
   * Description: Determines if the pattern @p is present in string @s. Can only
   * match extremely simple token=arg style patterns. If the pattern is found,
   * the location(s) of the arguments will be returned in the @args array.
   */
ef4533f8a   David Howells   [AFS]: Make the m...
26
  static int match_one(char *s, const char *p, substring_t args[])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  {
  	char *meta;
  	int argc = 0;
  
  	if (!p)
  		return 1;
  
  	while(1) {
  		int len = -1;
  		meta = strchr(p, '%');
  		if (!meta)
  			return strcmp(p, s) == 0;
  
  		if (strncmp(p, s, meta-p))
  			return 0;
  
  		s += meta - p;
  		p = meta + 1;
  
  		if (isdigit(*p))
ef4533f8a   David Howells   [AFS]: Make the m...
47
  			len = simple_strtoul(p, (char **) &p, 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
53
54
55
56
57
58
59
  		else if (*p == '%') {
  			if (*s++ != '%')
  				return 0;
  			p++;
  			continue;
  		}
  
  		if (argc >= MAX_OPT_ARGS)
  			return 0;
  
  		args[argc].from = s;
  		switch (*p++) {
b5f54b07c   André Goddard Rosa   parser: remove un...
60
61
62
63
  		case 's': {
  			size_t str_len = strlen(s);
  
  			if (str_len == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  				return 0;
b5f54b07c   André Goddard Rosa   parser: remove un...
65
66
  			if (len == -1 || len > str_len)
  				len = str_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
  			args[argc].to = s + len;
  			break;
b5f54b07c   André Goddard Rosa   parser: remove un...
69
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  		case 'd':
  			simple_strtol(s, &args[argc].to, 0);
  			goto num;
  		case 'u':
  			simple_strtoul(s, &args[argc].to, 0);
  			goto num;
  		case 'o':
  			simple_strtoul(s, &args[argc].to, 8);
  			goto num;
  		case 'x':
  			simple_strtoul(s, &args[argc].to, 16);
  		num:
  			if (args[argc].to == args[argc].from)
  				return 0;
  			break;
  		default:
  			return 0;
  		}
  		s = args[argc].to;
  		argc++;
  	}
  }
  
  /**
   * match_token: - Find a token (and optional args) in a string
   * @s: the string to examine for token/argument pairs
   * @table: match_table_t describing the set of allowed option tokens and the
   * arguments that may be associated with them. Must be terminated with a
   * &struct match_token whose pattern is set to the NULL pointer.
   * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
   * locations.
   *
   * Description: Detects which if any of a set of token strings has been passed
   * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
   * format identifiers which will be taken into account when matching the
   * tokens, and whose locations will be returned in the @args array.
   */
a447c0932   Steven Whitehouse   vfs: Use const fo...
107
  int match_token(char *s, const match_table_t table, substring_t args[])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  {
ef4533f8a   David Howells   [AFS]: Make the m...
109
  	const struct match_token *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
114
115
  
  	for (p = table; !match_one(s, p->pattern, args) ; p++)
  		;
  
  	return p->token;
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
116
  EXPORT_SYMBOL(match_token);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
120
121
122
123
124
125
  
  /**
   * match_number: scan a number in the given base from a substring_t
   * @s: substring to be scanned
   * @result: resulting integer on success
   * @base: base to use when converting string
   *
   * Description: Given a &substring_t and a base, attempts to parse the substring
   * as a number in that base. On success, sets @result to the integer represented
77dd3b0bd   Alex Elder   lib/parser.c: avo...
126
   * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
   */
  static int match_number(substring_t *s, int *result, int base)
  {
  	char *endp;
  	char *buf;
  	int ret;
77dd3b0bd   Alex Elder   lib/parser.c: avo...
133
  	long val;
5d051decf   Namhyung Kim   lib/parser: clean...
134
  	size_t len = s->to - s->from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135

5d051decf   Namhyung Kim   lib/parser: clean...
136
  	buf = kmalloc(len + 1, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
  	if (!buf)
  		return -ENOMEM;
5d051decf   Namhyung Kim   lib/parser: clean...
139
140
  	memcpy(buf, s->from, len);
  	buf[len] = '\0';
77dd3b0bd   Alex Elder   lib/parser.c: avo...
141

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  	ret = 0;
77dd3b0bd   Alex Elder   lib/parser.c: avo...
143
  	val = simple_strtol(buf, &endp, base);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
  	if (endp == buf)
  		ret = -EINVAL;
77dd3b0bd   Alex Elder   lib/parser.c: avo...
146
147
148
149
  	else if (val < (long)INT_MIN || val > (long)INT_MAX)
  		ret = -ERANGE;
  	else
  		*result = (int) val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
154
  	kfree(buf);
  	return ret;
  }
  
  /**
a317178e3   James Smart   parser: add u64 n...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
   * match_u64int: scan a number in the given base from a substring_t
   * @s: substring to be scanned
   * @result: resulting u64 on success
   * @base: base to use when converting string
   *
   * Description: Given a &substring_t and a base, attempts to parse the substring
   * as a number in that base. On success, sets @result to the integer represented
   * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
   */
  static int match_u64int(substring_t *s, u64 *result, int base)
  {
  	char *buf;
  	int ret;
  	u64 val;
  	size_t len = s->to - s->from;
  
  	buf = kmalloc(len + 1, GFP_KERNEL);
  	if (!buf)
  		return -ENOMEM;
  	memcpy(buf, s->from, len);
  	buf[len] = '\0';
  
  	ret = kstrtoull(buf, base, &val);
  	if (!ret)
  		*result = val;
  	kfree(buf);
  	return ret;
  }
  
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
187
188
189
190
   * match_int: - scan a decimal representation of an integer from a substring_t
   * @s: substring_t to be scanned
   * @result: resulting integer on success
   *
   * Description: Attempts to parse the &substring_t @s as a decimal integer. On
   * success, sets @result to the integer represented by the string and returns 0.
53769627b   Namjae Jeon   lib/parser.c: fix...
191
   * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
195
196
   */
  int match_int(substring_t *s, int *result)
  {
  	return match_number(s, result, 0);
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
197
  EXPORT_SYMBOL(match_int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
  
  /**
a317178e3   James Smart   parser: add u64 n...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
   * match_u64: - scan a decimal representation of a u64 from
   *                  a substring_t
   * @s: substring_t to be scanned
   * @result: resulting unsigned long long on success
   *
   * Description: Attempts to parse the &substring_t @s as a long decimal
   * integer. On success, sets @result to the integer represented by the
   * string and returns 0.
   * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
   */
  int match_u64(substring_t *s, u64 *result)
  {
  	return match_u64int(s, result, 0);
  }
  EXPORT_SYMBOL(match_u64);
  
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
219
220
221
222
   * match_octal: - scan an octal representation of an integer from a substring_t
   * @s: substring_t to be scanned
   * @result: resulting integer on success
   *
   * Description: Attempts to parse the &substring_t @s as an octal integer. On
   * success, sets @result to the integer represented by the string and returns
53769627b   Namjae Jeon   lib/parser.c: fix...
223
   * 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
227
228
   */
  int match_octal(substring_t *s, int *result)
  {
  	return match_number(s, result, 8);
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
229
  EXPORT_SYMBOL(match_octal);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
233
234
235
236
237
  
  /**
   * match_hex: - scan a hex representation of an integer from a substring_t
   * @s: substring_t to be scanned
   * @result: resulting integer on success
   *
   * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
   * On success, sets @result to the integer represented by the string and
53769627b   Namjae Jeon   lib/parser.c: fix...
238
   * returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
240
241
242
243
   */
  int match_hex(substring_t *s, int *result)
  {
  	return match_number(s, result, 16);
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
244
  EXPORT_SYMBOL(match_hex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
246
  
  /**
aace05097   Du, Changbin   lib/parser.c: add...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
   * match_wildcard: - parse if a string matches given wildcard pattern
   * @pattern: wildcard pattern
   * @str: the string to be parsed
   *
   * Description: Parse the string @str to check if matches wildcard
   * pattern @pattern. The pattern may contain two type wildcardes:
   *   '*' - matches zero or more characters
   *   '?' - matches one character
   * If it's matched, return true, else return false.
   */
  bool match_wildcard(const char *pattern, const char *str)
  {
  	const char *s = str;
  	const char *p = pattern;
  	bool star = false;
  
  	while (*s) {
  		switch (*p) {
  		case '?':
  			s++;
  			p++;
  			break;
  		case '*':
  			star = true;
  			str = s;
  			if (!*++p)
  				return true;
  			pattern = p;
  			break;
  		default:
  			if (*s == *p) {
  				s++;
  				p++;
  			} else {
  				if (!star)
  					return false;
  				str++;
  				s = str;
  				p = pattern;
  			}
  			break;
  		}
  	}
  
  	if (*p == '*')
  		++p;
  	return !*p;
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
295
  EXPORT_SYMBOL(match_wildcard);
aace05097   Du, Changbin   lib/parser.c: add...
296
297
  
  /**
b32a09db4   Markus Armbruster   add match_strlcpy...
298
299
300
301
   * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
   * @dest: where to copy to
   * @src: &substring_t to copy
   * @size: size of destination buffer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
   *
b32a09db4   Markus Armbruster   add match_strlcpy...
303
304
305
   * Description: Copy the characters in &substring_t @src to the
   * c-style string @dest.  Copy no more than @size - 1 characters, plus
   * the terminating NUL.  Return length of @src.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
   */
b32a09db4   Markus Armbruster   add match_strlcpy...
307
  size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
  {
b32a09db4   Markus Armbruster   add match_strlcpy...
309
310
311
312
313
314
315
316
  	size_t ret = src->to - src->from;
  
  	if (size) {
  		size_t len = ret >= size ? size - 1 : ret;
  		memcpy(dest, src->from, len);
  		dest[len] = '\0';
  	}
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  }
a3d2cca43   Andrew Morton   lib/parser.c: put...
318
  EXPORT_SYMBOL(match_strlcpy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
324
325
326
327
  
  /**
   * match_strdup: - allocate a new string with the contents of a substring_t
   * @s: &substring_t to copy
   *
   * Description: Allocates and returns a string filled with the contents of
   * the &substring_t @s. The caller is responsible for freeing the returned
   * string with kfree().
   */
ef4533f8a   David Howells   [AFS]: Make the m...
328
  char *match_strdup(const substring_t *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
  {
b32a09db4   Markus Armbruster   add match_strlcpy...
330
331
  	size_t sz = s->to - s->from + 1;
  	char *p = kmalloc(sz, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
  	if (p)
b32a09db4   Markus Armbruster   add match_strlcpy...
333
  		match_strlcpy(p, s, sz);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
  	return p;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
  EXPORT_SYMBOL(match_strdup);