parser.c 6.1 KB

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