PageRenderTime 916ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/parser.c

https://github.com/gby/linux
C | 336 lines | 189 code | 29 blank | 118 comment | 39 complexity | 2cf62c1f9d30c04c472782a1e938b14a MD5 | raw file
  1. /*
  2. * lib/parser.c - simple parser for mount, etc. options.
  3. *
  4. * This source code is licensed under the GNU General Public License,
  5. * Version 2. See the file COPYING for more details.
  6. */
  7. #include <linux/ctype.h>
  8. #include <linux/types.h>
  9. #include <linux/export.h>
  10. #include <linux/parser.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. /**
  14. * match_one: - Determines if a string matches a simple pattern
  15. * @s: the string to examine for presence of the pattern
  16. * @p: the string containing the pattern
  17. * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
  18. * locations.
  19. *
  20. * Description: Determines if the pattern @p is present in string @s. Can only
  21. * match extremely simple token=arg style patterns. If the pattern is found,
  22. * the location(s) of the arguments will be returned in the @args array.
  23. */
  24. static int match_one(char *s, const char *p, substring_t args[])
  25. {
  26. char *meta;
  27. int argc = 0;
  28. if (!p)
  29. return 1;
  30. while(1) {
  31. int len = -1;
  32. meta = strchr(p, '%');
  33. if (!meta)
  34. return strcmp(p, s) == 0;
  35. if (strncmp(p, s, meta-p))
  36. return 0;
  37. s += meta - p;
  38. p = meta + 1;
  39. if (isdigit(*p))
  40. len = simple_strtoul(p, (char **) &p, 10);
  41. else if (*p == '%') {
  42. if (*s++ != '%')
  43. return 0;
  44. p++;
  45. continue;
  46. }
  47. if (argc >= MAX_OPT_ARGS)
  48. return 0;
  49. args[argc].from = s;
  50. switch (*p++) {
  51. case 's': {
  52. size_t str_len = strlen(s);
  53. if (str_len == 0)
  54. return 0;
  55. if (len == -1 || len > str_len)
  56. len = str_len;
  57. args[argc].to = s + len;
  58. break;
  59. }
  60. case 'd':
  61. simple_strtol(s, &args[argc].to, 0);
  62. goto num;
  63. case 'u':
  64. simple_strtoul(s, &args[argc].to, 0);
  65. goto num;
  66. case 'o':
  67. simple_strtoul(s, &args[argc].to, 8);
  68. goto num;
  69. case 'x':
  70. simple_strtoul(s, &args[argc].to, 16);
  71. num:
  72. if (args[argc].to == args[argc].from)
  73. return 0;
  74. break;
  75. default:
  76. return 0;
  77. }
  78. s = args[argc].to;
  79. argc++;
  80. }
  81. }
  82. /**
  83. * match_token: - Find a token (and optional args) in a string
  84. * @s: the string to examine for token/argument pairs
  85. * @table: match_table_t describing the set of allowed option tokens and the
  86. * arguments that may be associated with them. Must be terminated with a
  87. * &struct match_token whose pattern is set to the NULL pointer.
  88. * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
  89. * locations.
  90. *
  91. * Description: Detects which if any of a set of token strings has been passed
  92. * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
  93. * format identifiers which will be taken into account when matching the
  94. * tokens, and whose locations will be returned in the @args array.
  95. */
  96. int match_token(char *s, const match_table_t table, substring_t args[])
  97. {
  98. const struct match_token *p;
  99. for (p = table; !match_one(s, p->pattern, args) ; p++)
  100. ;
  101. return p->token;
  102. }
  103. EXPORT_SYMBOL(match_token);
  104. /**
  105. * match_number: scan a number in the given base from a substring_t
  106. * @s: substring to be scanned
  107. * @result: resulting integer on success
  108. * @base: base to use when converting string
  109. *
  110. * Description: Given a &substring_t and a base, attempts to parse the substring
  111. * as a number in that base. On success, sets @result to the integer represented
  112. * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  113. */
  114. static int match_number(substring_t *s, int *result, int base)
  115. {
  116. char *endp;
  117. char *buf;
  118. int ret;
  119. long val;
  120. size_t len = s->to - s->from;
  121. buf = kmalloc(len + 1, GFP_KERNEL);
  122. if (!buf)
  123. return -ENOMEM;
  124. memcpy(buf, s->from, len);
  125. buf[len] = '\0';
  126. ret = 0;
  127. val = simple_strtol(buf, &endp, base);
  128. if (endp == buf)
  129. ret = -EINVAL;
  130. else if (val < (long)INT_MIN || val > (long)INT_MAX)
  131. ret = -ERANGE;
  132. else
  133. *result = (int) val;
  134. kfree(buf);
  135. return ret;
  136. }
  137. /**
  138. * match_u64int: scan a number in the given base from a substring_t
  139. * @s: substring to be scanned
  140. * @result: resulting u64 on success
  141. * @base: base to use when converting string
  142. *
  143. * Description: Given a &substring_t and a base, attempts to parse the substring
  144. * as a number in that base. On success, sets @result to the integer represented
  145. * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  146. */
  147. static int match_u64int(substring_t *s, u64 *result, int base)
  148. {
  149. char *buf;
  150. int ret;
  151. u64 val;
  152. size_t len = s->to - s->from;
  153. buf = kmalloc(len + 1, GFP_KERNEL);
  154. if (!buf)
  155. return -ENOMEM;
  156. memcpy(buf, s->from, len);
  157. buf[len] = '\0';
  158. ret = kstrtoull(buf, base, &val);
  159. if (!ret)
  160. *result = val;
  161. kfree(buf);
  162. return ret;
  163. }
  164. /**
  165. * match_int: - scan a decimal representation of an integer from a substring_t
  166. * @s: substring_t to be scanned
  167. * @result: resulting integer on success
  168. *
  169. * Description: Attempts to parse the &substring_t @s as a decimal integer. On
  170. * success, sets @result to the integer represented by the string and returns 0.
  171. * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  172. */
  173. int match_int(substring_t *s, int *result)
  174. {
  175. return match_number(s, result, 0);
  176. }
  177. EXPORT_SYMBOL(match_int);
  178. /**
  179. * match_u64: - scan a decimal representation of a u64 from
  180. * a substring_t
  181. * @s: substring_t to be scanned
  182. * @result: resulting unsigned long long on success
  183. *
  184. * Description: Attempts to parse the &substring_t @s as a long decimal
  185. * integer. On success, sets @result to the integer represented by the
  186. * string and returns 0.
  187. * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  188. */
  189. int match_u64(substring_t *s, u64 *result)
  190. {
  191. return match_u64int(s, result, 0);
  192. }
  193. EXPORT_SYMBOL(match_u64);
  194. /**
  195. * match_octal: - scan an octal representation of an integer from a substring_t
  196. * @s: substring_t to be scanned
  197. * @result: resulting integer on success
  198. *
  199. * Description: Attempts to parse the &substring_t @s as an octal integer. On
  200. * success, sets @result to the integer represented by the string and returns
  201. * 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  202. */
  203. int match_octal(substring_t *s, int *result)
  204. {
  205. return match_number(s, result, 8);
  206. }
  207. EXPORT_SYMBOL(match_octal);
  208. /**
  209. * match_hex: - scan a hex representation of an integer from a substring_t
  210. * @s: substring_t to be scanned
  211. * @result: resulting integer on success
  212. *
  213. * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
  214. * On success, sets @result to the integer represented by the string and
  215. * returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  216. */
  217. int match_hex(substring_t *s, int *result)
  218. {
  219. return match_number(s, result, 16);
  220. }
  221. EXPORT_SYMBOL(match_hex);
  222. /**
  223. * match_wildcard: - parse if a string matches given wildcard pattern
  224. * @pattern: wildcard pattern
  225. * @str: the string to be parsed
  226. *
  227. * Description: Parse the string @str to check if matches wildcard
  228. * pattern @pattern. The pattern may contain two type wildcardes:
  229. * '*' - matches zero or more characters
  230. * '?' - matches one character
  231. * If it's matched, return true, else return false.
  232. */
  233. bool match_wildcard(const char *pattern, const char *str)
  234. {
  235. const char *s = str;
  236. const char *p = pattern;
  237. bool star = false;
  238. while (*s) {
  239. switch (*p) {
  240. case '?':
  241. s++;
  242. p++;
  243. break;
  244. case '*':
  245. star = true;
  246. str = s;
  247. if (!*++p)
  248. return true;
  249. pattern = p;
  250. break;
  251. default:
  252. if (*s == *p) {
  253. s++;
  254. p++;
  255. } else {
  256. if (!star)
  257. return false;
  258. str++;
  259. s = str;
  260. p = pattern;
  261. }
  262. break;
  263. }
  264. }
  265. if (*p == '*')
  266. ++p;
  267. return !*p;
  268. }
  269. EXPORT_SYMBOL(match_wildcard);
  270. /**
  271. * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
  272. * @dest: where to copy to
  273. * @src: &substring_t to copy
  274. * @size: size of destination buffer
  275. *
  276. * Description: Copy the characters in &substring_t @src to the
  277. * c-style string @dest. Copy no more than @size - 1 characters, plus
  278. * the terminating NUL. Return length of @src.
  279. */
  280. size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
  281. {
  282. size_t ret = src->to - src->from;
  283. if (size) {
  284. size_t len = ret >= size ? size - 1 : ret;
  285. memcpy(dest, src->from, len);
  286. dest[len] = '\0';
  287. }
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(match_strlcpy);
  291. /**
  292. * match_strdup: - allocate a new string with the contents of a substring_t
  293. * @s: &substring_t to copy
  294. *
  295. * Description: Allocates and returns a string filled with the contents of
  296. * the &substring_t @s. The caller is responsible for freeing the returned
  297. * string with kfree().
  298. */
  299. char *match_strdup(const substring_t *s)
  300. {
  301. size_t sz = s->to - s->from + 1;
  302. char *p = kmalloc(sz, GFP_KERNEL);
  303. if (p)
  304. match_strlcpy(p, s, sz);
  305. return p;
  306. }
  307. EXPORT_SYMBOL(match_strdup);