parser.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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, 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, &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. if (strlen(s) == 0)
  52. return 0;
  53. else if (len == -1 || len > strlen(s))
  54. len = strlen(s);
  55. args[argc].to = s + len;
  56. break;
  57. case 'd':
  58. simple_strtol(s, &args[argc].to, 0);
  59. goto num;
  60. case 'u':
  61. simple_strtoul(s, &args[argc].to, 0);
  62. goto num;
  63. case 'o':
  64. simple_strtoul(s, &args[argc].to, 8);
  65. goto num;
  66. case 'x':
  67. simple_strtoul(s, &args[argc].to, 16);
  68. num:
  69. if (args[argc].to == args[argc].from)
  70. return 0;
  71. break;
  72. default:
  73. return 0;
  74. }
  75. s = args[argc].to;
  76. argc++;
  77. }
  78. }
  79. /**
  80. * match_token: - Find a token (and optional args) in a string
  81. * @s: the string to examine for token/argument pairs
  82. * @table: match_table_t describing the set of allowed option tokens and the
  83. * arguments that may be associated with them. Must be terminated with a
  84. * &struct match_token whose pattern is set to the NULL pointer.
  85. * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
  86. * locations.
  87. *
  88. * Description: Detects which if any of a set of token strings has been passed
  89. * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
  90. * format identifiers which will be taken into account when matching the
  91. * tokens, and whose locations will be returned in the @args array.
  92. */
  93. int match_token(char *s, match_table_t table, substring_t args[])
  94. {
  95. struct match_token *p;
  96. for (p = table; !match_one(s, p->pattern, args) ; p++)
  97. ;
  98. return p->token;
  99. }
  100. /**
  101. * match_number: scan a number in the given base from a substring_t
  102. * @s: substring to be scanned
  103. * @result: resulting integer on success
  104. * @base: base to use when converting string
  105. *
  106. * Description: Given a &substring_t and a base, attempts to parse the substring
  107. * as a number in that base. On success, sets @result to the integer represented
  108. * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
  109. */
  110. static int match_number(substring_t *s, int *result, int base)
  111. {
  112. char *endp;
  113. char *buf;
  114. int ret;
  115. buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
  116. if (!buf)
  117. return -ENOMEM;
  118. memcpy(buf, s->from, s->to - s->from);
  119. buf[s->to - s->from] = '\0';
  120. *result = simple_strtol(buf, &endp, base);
  121. ret = 0;
  122. if (endp == buf)
  123. ret = -EINVAL;
  124. kfree(buf);
  125. return ret;
  126. }
  127. /**
  128. * match_int: - scan a decimal representation of an integer from a substring_t
  129. * @s: substring_t to be scanned
  130. * @result: resulting integer on success
  131. *
  132. * Description: Attempts to parse the &substring_t @s as a decimal integer. On
  133. * success, sets @result to the integer represented by the string and returns 0.
  134. * Returns either -ENOMEM or -EINVAL on failure.
  135. */
  136. int match_int(substring_t *s, int *result)
  137. {
  138. return match_number(s, result, 0);
  139. }
  140. /**
  141. * match_octal: - scan an octal representation of an integer from a substring_t
  142. * @s: substring_t to be scanned
  143. * @result: resulting integer on success
  144. *
  145. * Description: Attempts to parse the &substring_t @s as an octal integer. On
  146. * success, sets @result to the integer represented by the string and returns
  147. * 0. Returns either -ENOMEM or -EINVAL on failure.
  148. */
  149. int match_octal(substring_t *s, int *result)
  150. {
  151. return match_number(s, result, 8);
  152. }
  153. /**
  154. * match_hex: - scan a hex representation of an integer from a substring_t
  155. * @s: substring_t to be scanned
  156. * @result: resulting integer on success
  157. *
  158. * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
  159. * On success, sets @result to the integer represented by the string and
  160. * returns 0. Returns either -ENOMEM or -EINVAL on failure.
  161. */
  162. int match_hex(substring_t *s, int *result)
  163. {
  164. return match_number(s, result, 16);
  165. }
  166. /**
  167. * match_strcpy: - copies the characters from a substring_t to a string
  168. * @to: string to copy characters to.
  169. * @s: &substring_t to copy
  170. *
  171. * Description: Copies the set of characters represented by the given
  172. * &substring_t @s to the c-style string @to. Caller guarantees that @to is
  173. * large enough to hold the characters of @s.
  174. */
  175. void match_strcpy(char *to, substring_t *s)
  176. {
  177. memcpy(to, s->from, s->to - s->from);
  178. to[s->to - s->from] = '\0';
  179. }
  180. /**
  181. * match_strdup: - allocate a new string with the contents of a substring_t
  182. * @s: &substring_t to copy
  183. *
  184. * Description: Allocates and returns a string filled with the contents of
  185. * the &substring_t @s. The caller is responsible for freeing the returned
  186. * string with kfree().
  187. */
  188. char *match_strdup(substring_t *s)
  189. {
  190. char *p = kmalloc(s->to - s->from + 1, GFP_KERNEL);
  191. if (p)
  192. match_strcpy(p, s);
  193. return p;
  194. }
  195. EXPORT_SYMBOL(match_token);
  196. EXPORT_SYMBOL(match_int);
  197. EXPORT_SYMBOL(match_octal);
  198. EXPORT_SYMBOL(match_hex);
  199. EXPORT_SYMBOL(match_strcpy);
  200. EXPORT_SYMBOL(match_strdup);