cmdline.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/lib/cmdline.c
  4. * Helper functions generally used for parsing kernel command line
  5. * and module options.
  6. *
  7. * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
  8. *
  9. * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
  10. */
  11. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/ctype.h>
  15. /*
  16. * If a hyphen was found in get_option, this will handle the
  17. * range of numbers, M-N. This will expand the range and insert
  18. * the values[M, M+1, ..., N] into the ints array in get_options.
  19. */
  20. static int get_range(char **str, int *pint, int n)
  21. {
  22. int x, inc_counter, upper_range;
  23. (*str)++;
  24. upper_range = simple_strtol((*str), NULL, 0);
  25. inc_counter = upper_range - *pint;
  26. for (x = *pint; n && x < upper_range; x++, n--)
  27. *pint++ = x;
  28. return inc_counter;
  29. }
  30. /**
  31. * get_option - Parse integer from an option string
  32. * @str: option string
  33. * @pint: (optional output) integer value parsed from @str
  34. *
  35. * Read an int from an option string; if available accept a subsequent
  36. * comma as well.
  37. *
  38. * When @pint is NULL the function can be used as a validator of
  39. * the current option in the string.
  40. *
  41. * Return values:
  42. * 0 - no int in string
  43. * 1 - int found, no subsequent comma
  44. * 2 - int found including a subsequent comma
  45. * 3 - hyphen found to denote a range
  46. *
  47. * Leading hyphen without integer is no integer case, but we consume it
  48. * for the sake of simplification.
  49. */
  50. int get_option(char **str, int *pint)
  51. {
  52. char *cur = *str;
  53. int value;
  54. if (!cur || !(*cur))
  55. return 0;
  56. if (*cur == '-')
  57. value = -simple_strtoull(++cur, str, 0);
  58. else
  59. value = simple_strtoull(cur, str, 0);
  60. if (pint)
  61. *pint = value;
  62. if (cur == *str)
  63. return 0;
  64. if (**str == ',') {
  65. (*str)++;
  66. return 2;
  67. }
  68. if (**str == '-')
  69. return 3;
  70. return 1;
  71. }
  72. EXPORT_SYMBOL(get_option);
  73. /**
  74. * get_options - Parse a string into a list of integers
  75. * @str: String to be parsed
  76. * @nints: size of integer array
  77. * @ints: integer array (must have room for at least one element)
  78. *
  79. * This function parses a string containing a comma-separated
  80. * list of integers, a hyphen-separated range of _positive_ integers,
  81. * or a combination of both. The parse halts when the array is
  82. * full, or when no more numbers can be retrieved from the
  83. * string.
  84. *
  85. * When @nints is 0, the function just validates the given @str and
  86. * returns the amount of parseable integers as described below.
  87. *
  88. * Returns:
  89. *
  90. * The first element is filled by the number of collected integers
  91. * in the range. The rest is what was parsed from the @str.
  92. *
  93. * Return value is the character in the string which caused
  94. * the parse to end (typically a null terminator, if @str is
  95. * completely parseable).
  96. */
  97. char *get_options(const char *str, int nints, int *ints)
  98. {
  99. bool validate = (nints == 0);
  100. int res, i = 1;
  101. while (i < nints || validate) {
  102. int *pint = validate ? ints : ints + i;
  103. res = get_option((char **)&str, pint);
  104. if (res == 0)
  105. break;
  106. if (res == 3) {
  107. int n = validate ? 0 : nints - i;
  108. int range_nums;
  109. range_nums = get_range((char **)&str, pint, n);
  110. if (range_nums < 0)
  111. break;
  112. /*
  113. * Decrement the result by one to leave out the
  114. * last number in the range. The next iteration
  115. * will handle the upper number in the range
  116. */
  117. i += (range_nums - 1);
  118. }
  119. i++;
  120. if (res == 1)
  121. break;
  122. }
  123. ints[0] = i - 1;
  124. return (char *)str;
  125. }
  126. EXPORT_SYMBOL(get_options);
  127. /**
  128. * memparse - parse a string with mem suffixes into a number
  129. * @ptr: Where parse begins
  130. * @retptr: (output) Optional pointer to next char after parse completes
  131. *
  132. * Parses a string into a number. The number stored at @ptr is
  133. * potentially suffixed with K, M, G, T, P, E.
  134. */
  135. unsigned long long memparse(const char *ptr, char **retptr)
  136. {
  137. char *endptr; /* local pointer to end of parsed string */
  138. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  139. switch (*endptr) {
  140. case 'E':
  141. case 'e':
  142. ret <<= 10;
  143. fallthrough;
  144. case 'P':
  145. case 'p':
  146. ret <<= 10;
  147. fallthrough;
  148. case 'T':
  149. case 't':
  150. ret <<= 10;
  151. fallthrough;
  152. case 'G':
  153. case 'g':
  154. ret <<= 10;
  155. fallthrough;
  156. case 'M':
  157. case 'm':
  158. ret <<= 10;
  159. fallthrough;
  160. case 'K':
  161. case 'k':
  162. ret <<= 10;
  163. endptr++;
  164. fallthrough;
  165. default:
  166. break;
  167. }
  168. if (retptr)
  169. *retptr = endptr;
  170. return ret;
  171. }
  172. EXPORT_SYMBOL(memparse);
  173. /**
  174. * parse_option_str - Parse a string and check an option is set or not
  175. * @str: String to be parsed
  176. * @option: option name
  177. *
  178. * This function parses a string containing a comma-separated list of
  179. * strings like a=b,c.
  180. *
  181. * Return true if there's such option in the string, or return false.
  182. */
  183. bool parse_option_str(const char *str, const char *option)
  184. {
  185. while (*str) {
  186. if (!strncmp(str, option, strlen(option))) {
  187. str += strlen(option);
  188. if (!*str || *str == ',')
  189. return true;
  190. }
  191. while (*str && *str != ',')
  192. str++;
  193. if (*str == ',')
  194. str++;
  195. }
  196. return false;
  197. }
  198. /*
  199. * Parse a string to get a param value pair.
  200. * You can use " around spaces, but can't escape ".
  201. * Hyphens and underscores equivalent in parameter names.
  202. */
  203. char *next_arg(char *args, char **param, char **val)
  204. {
  205. unsigned int i, equals = 0;
  206. int in_quote = 0, quoted = 0;
  207. if (*args == '"') {
  208. args++;
  209. in_quote = 1;
  210. quoted = 1;
  211. }
  212. for (i = 0; args[i]; i++) {
  213. if (isspace(args[i]) && !in_quote)
  214. break;
  215. if (equals == 0) {
  216. if (args[i] == '=')
  217. equals = i;
  218. }
  219. if (args[i] == '"')
  220. in_quote = !in_quote;
  221. }
  222. *param = args;
  223. if (!equals)
  224. *val = NULL;
  225. else {
  226. args[equals] = '\0';
  227. *val = args + equals + 1;
  228. /* Don't include quotes in value. */
  229. if (**val == '"') {
  230. (*val)++;
  231. if (args[i-1] == '"')
  232. args[i-1] = '\0';
  233. }
  234. }
  235. if (quoted && i > 0 && args[i-1] == '"')
  236. args[i-1] = '\0';
  237. if (args[i]) {
  238. args[i] = '\0';
  239. args += i + 1;
  240. } else
  241. args += i;
  242. /* Chew up trailing spaces. */
  243. return skip_spaces(args);
  244. }
  245. EXPORT_SYMBOL(next_arg);