parser.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 presence 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. size_t len = s->to - s->from;
  118. buf = kmalloc(len + 1, GFP_KERNEL);
  119. if (!buf)
  120. return -ENOMEM;
  121. memcpy(buf, s->from, len);
  122. buf[len] = '\0';
  123. *result = simple_strtol(buf, &endp, base);
  124. ret = 0;
  125. if (endp == buf)
  126. ret = -EINVAL;
  127. kfree(buf);
  128. return ret;
  129. }
  130. /**
  131. * match_int: - scan a decimal representation of an integer from a substring_t
  132. * @s: substring_t to be scanned
  133. * @result: resulting integer on success
  134. *
  135. * Description: Attempts to parse the &substring_t @s as a decimal integer. On
  136. * success, sets @result to the integer represented by the string and returns 0.
  137. * Returns either -ENOMEM or -EINVAL on failure.
  138. */
  139. int match_int(substring_t *s, int *result)
  140. {
  141. return match_number(s, result, 0);
  142. }
  143. /**
  144. * match_octal: - scan an octal representation of an integer from a substring_t
  145. * @s: substring_t to be scanned
  146. * @result: resulting integer on success
  147. *
  148. * Description: Attempts to parse the &substring_t @s as an octal integer. On
  149. * success, sets @result to the integer represented by the string and returns
  150. * 0. Returns either -ENOMEM or -EINVAL on failure.
  151. */
  152. int match_octal(substring_t *s, int *result)
  153. {
  154. return match_number(s, result, 8);
  155. }
  156. /**
  157. * match_hex: - scan a hex representation of an integer from a substring_t
  158. * @s: substring_t to be scanned
  159. * @result: resulting integer on success
  160. *
  161. * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
  162. * On success, sets @result to the integer represented by the string and
  163. * returns 0. Returns either -ENOMEM or -EINVAL on failure.
  164. */
  165. int match_hex(substring_t *s, int *result)
  166. {
  167. return match_number(s, result, 16);
  168. }
  169. /**
  170. * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
  171. * @dest: where to copy to
  172. * @src: &substring_t to copy
  173. * @size: size of destination buffer
  174. *
  175. * Description: Copy the characters in &substring_t @src to the
  176. * c-style string @dest. Copy no more than @size - 1 characters, plus
  177. * the terminating NUL. Return length of @src.
  178. */
  179. size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
  180. {
  181. size_t ret = src->to - src->from;
  182. if (size) {
  183. size_t len = ret >= size ? size - 1 : ret;
  184. memcpy(dest, src->from, len);
  185. dest[len] = '\0';
  186. }
  187. return ret;
  188. }
  189. /**
  190. * match_strdup: - allocate a new string with the contents of a substring_t
  191. * @s: &substring_t to copy
  192. *
  193. * Description: Allocates and returns a string filled with the contents of
  194. * the &substring_t @s. The caller is responsible for freeing the returned
  195. * string with kfree().
  196. */
  197. char *match_strdup(const substring_t *s)
  198. {
  199. size_t sz = s->to - s->from + 1;
  200. char *p = kmalloc(sz, GFP_KERNEL);
  201. if (p)
  202. match_strlcpy(p, s, sz);
  203. return p;
  204. }
  205. EXPORT_SYMBOL(match_token);
  206. EXPORT_SYMBOL(match_int);
  207. EXPORT_SYMBOL(match_octal);
  208. EXPORT_SYMBOL(match_hex);
  209. EXPORT_SYMBOL(match_strlcpy);
  210. EXPORT_SYMBOL(match_strdup);