parser.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_int: - scan a decimal representation of an integer from a substring_t
  139. * @s: substring_t to be scanned
  140. * @result: resulting integer on success
  141. *
  142. * Description: Attempts to parse the &substring_t @s as a decimal integer. On
  143. * success, sets @result to the integer represented by the string and returns 0.
  144. * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  145. */
  146. int match_int(substring_t *s, int *result)
  147. {
  148. return match_number(s, result, 0);
  149. }
  150. EXPORT_SYMBOL(match_int);
  151. /**
  152. * match_octal: - scan an octal representation of an integer from a substring_t
  153. * @s: substring_t to be scanned
  154. * @result: resulting integer on success
  155. *
  156. * Description: Attempts to parse the &substring_t @s as an octal integer. On
  157. * success, sets @result to the integer represented by the string and returns
  158. * 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  159. */
  160. int match_octal(substring_t *s, int *result)
  161. {
  162. return match_number(s, result, 8);
  163. }
  164. EXPORT_SYMBOL(match_octal);
  165. /**
  166. * match_hex: - scan a hex representation of an integer from a substring_t
  167. * @s: substring_t to be scanned
  168. * @result: resulting integer on success
  169. *
  170. * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
  171. * On success, sets @result to the integer represented by the string and
  172. * returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
  173. */
  174. int match_hex(substring_t *s, int *result)
  175. {
  176. return match_number(s, result, 16);
  177. }
  178. EXPORT_SYMBOL(match_hex);
  179. /**
  180. * match_wildcard: - parse if a string matches given wildcard pattern
  181. * @pattern: wildcard pattern
  182. * @str: the string to be parsed
  183. *
  184. * Description: Parse the string @str to check if matches wildcard
  185. * pattern @pattern. The pattern may contain two type wildcardes:
  186. * '*' - matches zero or more characters
  187. * '?' - matches one character
  188. * If it's matched, return true, else return false.
  189. */
  190. bool match_wildcard(const char *pattern, const char *str)
  191. {
  192. const char *s = str;
  193. const char *p = pattern;
  194. bool star = false;
  195. while (*s) {
  196. switch (*p) {
  197. case '?':
  198. s++;
  199. p++;
  200. break;
  201. case '*':
  202. star = true;
  203. str = s;
  204. if (!*++p)
  205. return true;
  206. pattern = p;
  207. break;
  208. default:
  209. if (*s == *p) {
  210. s++;
  211. p++;
  212. } else {
  213. if (!star)
  214. return false;
  215. str++;
  216. s = str;
  217. p = pattern;
  218. }
  219. break;
  220. }
  221. }
  222. if (*p == '*')
  223. ++p;
  224. return !*p;
  225. }
  226. EXPORT_SYMBOL(match_wildcard);
  227. /**
  228. * match_strlcpy: - Copy the characters from a substring_t to a sized buffer
  229. * @dest: where to copy to
  230. * @src: &substring_t to copy
  231. * @size: size of destination buffer
  232. *
  233. * Description: Copy the characters in &substring_t @src to the
  234. * c-style string @dest. Copy no more than @size - 1 characters, plus
  235. * the terminating NUL. Return length of @src.
  236. */
  237. size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
  238. {
  239. size_t ret = src->to - src->from;
  240. if (size) {
  241. size_t len = ret >= size ? size - 1 : ret;
  242. memcpy(dest, src->from, len);
  243. dest[len] = '\0';
  244. }
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(match_strlcpy);
  248. /**
  249. * match_strdup: - allocate a new string with the contents of a substring_t
  250. * @s: &substring_t to copy
  251. *
  252. * Description: Allocates and returns a string filled with the contents of
  253. * the &substring_t @s. The caller is responsible for freeing the returned
  254. * string with kfree().
  255. */
  256. char *match_strdup(const substring_t *s)
  257. {
  258. size_t sz = s->to - s->from + 1;
  259. char *p = kmalloc(sz, GFP_KERNEL);
  260. if (p)
  261. match_strlcpy(p, s, sz);
  262. return p;
  263. }
  264. EXPORT_SYMBOL(match_strdup);