cmdline.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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: (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. * Return values:
  39. * 0 - no int in string
  40. * 1 - int found, no subsequent comma
  41. * 2 - int found including a subsequent comma
  42. * 3 - hyphen found to denote a range
  43. */
  44. int get_option(char **str, int *pint)
  45. {
  46. char *cur = *str;
  47. if (!cur || !(*cur))
  48. return 0;
  49. *pint = simple_strtol(cur, str, 0);
  50. if (cur == *str)
  51. return 0;
  52. if (**str == ',') {
  53. (*str)++;
  54. return 2;
  55. }
  56. if (**str == '-')
  57. return 3;
  58. return 1;
  59. }
  60. EXPORT_SYMBOL(get_option);
  61. /**
  62. * get_options - Parse a string into a list of integers
  63. * @str: String to be parsed
  64. * @nints: size of integer array
  65. * @ints: integer array
  66. *
  67. * This function parses a string containing a comma-separated
  68. * list of integers, a hyphen-separated range of _positive_ integers,
  69. * or a combination of both. The parse halts when the array is
  70. * full, or when no more numbers can be retrieved from the
  71. * string.
  72. *
  73. * Return value is the character in the string which caused
  74. * the parse to end (typically a null terminator, if @str is
  75. * completely parseable).
  76. */
  77. char *get_options(const char *str, int nints, int *ints)
  78. {
  79. int res, i = 1;
  80. while (i < nints) {
  81. res = get_option((char **)&str, ints + i);
  82. if (res == 0)
  83. break;
  84. if (res == 3) {
  85. int range_nums;
  86. range_nums = get_range((char **)&str, ints + i, nints - i);
  87. if (range_nums < 0)
  88. break;
  89. /*
  90. * Decrement the result by one to leave out the
  91. * last number in the range. The next iteration
  92. * will handle the upper number in the range
  93. */
  94. i += (range_nums - 1);
  95. }
  96. i++;
  97. if (res == 1)
  98. break;
  99. }
  100. ints[0] = i - 1;
  101. return (char *)str;
  102. }
  103. EXPORT_SYMBOL(get_options);
  104. /**
  105. * memparse - parse a string with mem suffixes into a number
  106. * @ptr: Where parse begins
  107. * @retptr: (output) Optional pointer to next char after parse completes
  108. *
  109. * Parses a string into a number. The number stored at @ptr is
  110. * potentially suffixed with K, M, G, T, P, E.
  111. */
  112. unsigned long long memparse(const char *ptr, char **retptr)
  113. {
  114. char *endptr; /* local pointer to end of parsed string */
  115. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  116. switch (*endptr) {
  117. case 'E':
  118. case 'e':
  119. ret <<= 10;
  120. /* fall through */
  121. case 'P':
  122. case 'p':
  123. ret <<= 10;
  124. /* fall through */
  125. case 'T':
  126. case 't':
  127. ret <<= 10;
  128. /* fall through */
  129. case 'G':
  130. case 'g':
  131. ret <<= 10;
  132. /* fall through */
  133. case 'M':
  134. case 'm':
  135. ret <<= 10;
  136. /* fall through */
  137. case 'K':
  138. case 'k':
  139. ret <<= 10;
  140. endptr++;
  141. default:
  142. break;
  143. }
  144. if (retptr)
  145. *retptr = endptr;
  146. return ret;
  147. }
  148. EXPORT_SYMBOL(memparse);
  149. /**
  150. * parse_option_str - Parse a string and check an option is set or not
  151. * @str: String to be parsed
  152. * @option: option name
  153. *
  154. * This function parses a string containing a comma-separated list of
  155. * strings like a=b,c.
  156. *
  157. * Return true if there's such option in the string, or return false.
  158. */
  159. bool parse_option_str(const char *str, const char *option)
  160. {
  161. while (*str) {
  162. if (!strncmp(str, option, strlen(option))) {
  163. str += strlen(option);
  164. if (!*str || *str == ',')
  165. return true;
  166. }
  167. while (*str && *str != ',')
  168. str++;
  169. if (*str == ',')
  170. str++;
  171. }
  172. return false;
  173. }
  174. /*
  175. * Parse a string to get a param value pair.
  176. * You can use " around spaces, but can't escape ".
  177. * Hyphens and underscores equivalent in parameter names.
  178. */
  179. char *next_arg(char *args, char **param, char **val)
  180. {
  181. unsigned int i, equals = 0;
  182. int in_quote = 0, quoted = 0;
  183. char *next;
  184. if (*args == '"') {
  185. args++;
  186. in_quote = 1;
  187. quoted = 1;
  188. }
  189. for (i = 0; args[i]; i++) {
  190. if (isspace(args[i]) && !in_quote)
  191. break;
  192. if (equals == 0) {
  193. if (args[i] == '=')
  194. equals = i;
  195. }
  196. if (args[i] == '"')
  197. in_quote = !in_quote;
  198. }
  199. *param = args;
  200. if (!equals)
  201. *val = NULL;
  202. else {
  203. args[equals] = '\0';
  204. *val = args + equals + 1;
  205. /* Don't include quotes in value. */
  206. if (**val == '"') {
  207. (*val)++;
  208. if (args[i-1] == '"')
  209. args[i-1] = '\0';
  210. }
  211. }
  212. if (quoted && args[i-1] == '"')
  213. args[i-1] = '\0';
  214. if (args[i]) {
  215. args[i] = '\0';
  216. next = args + i + 1;
  217. } else
  218. next = args + i;
  219. /* Chew up trailing spaces. */
  220. return skip_spaces(next);
  221. }