PageRenderTime 19ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/parser.c

https://bitbucket.org/abioy/linux
C | 231 lines | 123 code | 22 blank | 86 comment | 25 complexity | dcadc407e4805b8a6c17bd372df3d816 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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/module.h>
  9. #include <linux/parser.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. /**
  13. * match_one: - Determines if a string matches a simple pattern
  14. * @s: the string to examine for presense of the pattern
  15. * @p: the string containing the pattern
  16. * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
  17. * locations.
  18. *
  19. * Description: Determines if the pattern @p is present in string @s. Can only
  20. * match extremely simple token=arg style patterns. If the pattern is found,
  21. * the location(s) of the arguments will be returned in the @args array.
  22. */
  23. static int match_one(char *s, const char *p, substring_t args[])
  24. {
  25. char *meta;
  26. int argc = 0;
  27. if (!p)
  28. return 1;
  29. while(1) {
  30. int len = -1;
  31. meta = strchr(p, '%');
  32. if (!meta)
  33. return strcmp(p, s) == 0;
  34. if (strncmp(p, s, meta-p))
  35. return 0;
  36. s += meta - p;
  37. p = meta + 1;
  38. if (isdigit(*p))
  39. len = simple_strtoul(p, (char **) &p, 10);
  40. else if (*p == '%') {
  41. if (*s++ != '%')
  42. return 0;
  43. p++;
  44. continue;
  45. }
  46. if (argc >= MAX_OPT_ARGS)
  47. return 0;
  48. args[argc].from = s;
  49. switch (*p++) {
  50. case 's': {
  51. size_t str_len = strlen(s);
  52. if (str_len == 0)
  53. return 0;
  54. if (len == -1 || len > str_len)
  55. len = str_len;
  56. args[argc].to = s + len;
  57. break;
  58. }
  59. case 'd':
  60. simple_strtol(s, &args[argc].to, 0);
  61. goto num;
  62. case 'u':
  63. simple_strtoul(s, &args[argc].to, 0);
  64. goto num;
  65. case 'o':
  66. simple_strtoul(s, &args[argc].to, 8);
  67. goto num;
  68. case 'x':
  69. simple_strtoul(s, &args[argc].to, 16);
  70. num:
  71. if (args[argc].to == args[argc].from)
  72. return 0;
  73. break;
  74. default:
  75. return 0;
  76. }
  77. s = args[argc].to;
  78. argc++;
  79. }
  80. }
  81. /**
  82. * match_token: - Find a token (and optional args) in a string
  83. * @s: the string to examine for token/argument pairs
  84. * @table: match_table_t describing the set of allowed option tokens and the
  85. * arguments that may be associated with them. Must be terminated with a
  86. * &struct match_token whose pattern is set to the NULL pointer.
  87. * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
  88. * locations.
  89. *
  90. * Description: Detects which if any of a set of token strings has been passed
  91. * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
  92. * format identifiers which will be taken into account when matching the
  93. * tokens, and whose locations will be returned in the @args array.
  94. */
  95. int match_token(char *s, const match_table_t table, substring_t args[])
  96. {
  97. const struct match_token *p;
  98. for (p = table; !match_one(s, p->pattern, args) ; p++)
  99. ;
  100. return p->token;
  101. }
  102. /**
  103. * match_number: scan a number in the given base from a substring_t
  104. * @s: substring to be scanned
  105. * @result: resulting integer on success
  106. * @base: base to use when converting string
  107. *
  108. * Description: Given a &substring_t and a base, attempts to parse the substring
  109. * as a number in that base. On success, sets @result to the integer represented
  110. * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
  111. */
  112. static int match_number(substring_t *s, int *result, int base)
  113. {
  114. char *endp;
  115. char *buf;
  116. int ret;
  117. buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
  118. if (!buf)
  119. return -ENOMEM;
  120. memcpy(buf, s->from, s->to - s->from);
  121. buf[s->to - s->from] = '\0';
  122. *result = simple_strtol(buf, &endp, base);
  123. ret = 0;
  124. if (endp == buf)
  125. ret = -EINVAL;
  126. kfree(buf);
  127. return ret;
  128. }
  129. /**
  130. * match_int: - scan a decimal representation of an integer from a substring_t
  131. * @s: substring_t to be scanned
  132. * @result: resulting integer on success
  133. *
  134. * Description: Attempts to parse the &substring_t @s as a decimal integer. On
  135. * success, sets @result to the integer represented by the string and returns 0.
  136. * Returns either -ENOMEM or -EINVAL on failure.
  137. */
  138. int match_int(substring_t *s, int *result)
  139. {
  140. return match_number(s, result, 0);
  141. }
  142. /**
  143. * match_octal: - scan an octal representation of an integer from a substring_t
  144. * @s: substring_t to be scanned
  145. * @result: resulting integer on success
  146. *
  147. * Description: Attempts to parse the &substring_t @s as an octal integer. On
  148. * success, sets @result to the integer represented by the string and returns
  149. * 0. Returns either -ENOMEM or -EINVAL on failure.
  150. */
  151. int match_octal(substring_t *s, int *result)
  152. {
  153. return match_number(s, result, 8);
  154. }
  155. /**
  156. * match_hex: - scan a hex representation of an integer from a substring_t
  157. * @s: substring_t to be scanned
  158. * @result: resulting integer on success
  159. *
  160. * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
  161. * On success, sets @result to the integer represented by the string and
  162. * returns 0. Returns either -ENOMEM or -EINVAL on failure.
  163. */
  164. int match_hex(substring_t *s, int *result)
  165. {
  166. return match_number(s, result, 16);
  167. }
  168. /**
  169. * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
  170. * @dest: where to copy to
  171. * @src: &substring_t to copy
  172. * @size: size of destination buffer
  173. *
  174. * Description: Copy the characters in &substring_t @src to the
  175. * c-style string @dest. Copy no more than @size - 1 characters, plus
  176. * the terminating NUL. Return length of @src.
  177. */
  178. size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
  179. {
  180. size_t ret = src->to - src->from;
  181. if (size) {
  182. size_t len = ret >= size ? size - 1 : ret;
  183. memcpy(dest, src->from, len);
  184. dest[len] = '\0';
  185. }
  186. return ret;
  187. }
  188. /**
  189. * match_strdup: - allocate a new string with the contents of a substring_t
  190. * @s: &substring_t to copy
  191. *
  192. * Description: Allocates and returns a string filled with the contents of
  193. * the &substring_t @s. The caller is responsible for freeing the returned
  194. * string with kfree().
  195. */
  196. char *match_strdup(const substring_t *s)
  197. {
  198. size_t sz = s->to - s->from + 1;
  199. char *p = kmalloc(sz, GFP_KERNEL);
  200. if (p)
  201. match_strlcpy(p, s, sz);
  202. return p;
  203. }
  204. EXPORT_SYMBOL(match_token);
  205. EXPORT_SYMBOL(match_int);
  206. EXPORT_SYMBOL(match_octal);
  207. EXPORT_SYMBOL(match_hex);
  208. EXPORT_SYMBOL(match_strlcpy);
  209. EXPORT_SYMBOL(match_strdup);