cmdline.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * linux/lib/cmdline.c
  3. * Helper functions generally used for parsing kernel command line
  4. * and module options.
  5. *
  6. * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2. See the file COPYING for more details.
  10. *
  11. * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
  12. *
  13. */
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. /*
  18. * If a hyphen was found in get_option, this will handle the
  19. * range of numbers, M-N. This will expand the range and insert
  20. * the values[M, M+1, ..., N] into the ints array in get_options.
  21. */
  22. static int get_range(char **str, int *pint, int n)
  23. {
  24. int x, inc_counter, upper_range;
  25. (*str)++;
  26. upper_range = simple_strtol((*str), NULL, 0);
  27. inc_counter = upper_range - *pint;
  28. for (x = *pint; n && x < upper_range; x++, n--)
  29. *pint++ = x;
  30. return inc_counter;
  31. }
  32. /**
  33. * get_option - Parse integer from an option string
  34. * @str: option string
  35. * @pint: (output) integer value parsed from @str
  36. *
  37. * Read an int from an option string; if available accept a subsequent
  38. * comma as well.
  39. *
  40. * Return values:
  41. * 0 - no int in string
  42. * 1 - int found, no subsequent comma
  43. * 2 - int found including a subsequent comma
  44. * 3 - hyphen found to denote a range
  45. */
  46. int get_option(char **str, int *pint)
  47. {
  48. char *cur = *str;
  49. if (!cur || !(*cur))
  50. return 0;
  51. *pint = simple_strtol(cur, str, 0);
  52. if (cur == *str)
  53. return 0;
  54. if (**str == ',') {
  55. (*str)++;
  56. return 2;
  57. }
  58. if (**str == '-')
  59. return 3;
  60. return 1;
  61. }
  62. EXPORT_SYMBOL(get_option);
  63. /**
  64. * get_options - Parse a string into a list of integers
  65. * @str: String to be parsed
  66. * @nints: size of integer array
  67. * @ints: integer array
  68. *
  69. * This function parses a string containing a comma-separated
  70. * list of integers, a hyphen-separated range of _positive_ integers,
  71. * or a combination of both. The parse halts when the array is
  72. * full, or when no more numbers can be retrieved from the
  73. * string.
  74. *
  75. * Return value is the character in the string which caused
  76. * the parse to end (typically a null terminator, if @str is
  77. * completely parseable).
  78. */
  79. char *get_options(const char *str, int nints, int *ints)
  80. {
  81. int res, i = 1;
  82. while (i < nints) {
  83. res = get_option((char **)&str, ints + i);
  84. if (res == 0)
  85. break;
  86. if (res == 3) {
  87. int range_nums;
  88. range_nums = get_range((char **)&str, ints + i, nints - i);
  89. if (range_nums < 0)
  90. break;
  91. /*
  92. * Decrement the result by one to leave out the
  93. * last number in the range. The next iteration
  94. * will handle the upper number in the range
  95. */
  96. i += (range_nums - 1);
  97. }
  98. i++;
  99. if (res == 1)
  100. break;
  101. }
  102. ints[0] = i - 1;
  103. return (char *)str;
  104. }
  105. EXPORT_SYMBOL(get_options);
  106. /**
  107. * memparse - parse a string with mem suffixes into a number
  108. * @ptr: Where parse begins
  109. * @retptr: (output) Optional pointer to next char after parse completes
  110. *
  111. * Parses a string into a number. The number stored at @ptr is
  112. * potentially suffixed with K, M, G, T, P, E.
  113. */
  114. unsigned long long memparse(const char *ptr, char **retptr)
  115. {
  116. char *endptr; /* local pointer to end of parsed string */
  117. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  118. switch (*endptr) {
  119. case 'E':
  120. case 'e':
  121. ret <<= 10;
  122. case 'P':
  123. case 'p':
  124. ret <<= 10;
  125. case 'T':
  126. case 't':
  127. ret <<= 10;
  128. case 'G':
  129. case 'g':
  130. ret <<= 10;
  131. case 'M':
  132. case 'm':
  133. ret <<= 10;
  134. case 'K':
  135. case 'k':
  136. ret <<= 10;
  137. endptr++;
  138. default:
  139. break;
  140. }
  141. if (retptr)
  142. *retptr = endptr;
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(memparse);
  146. /**
  147. * parse_option_str - Parse a string and check an option is set or not
  148. * @str: String to be parsed
  149. * @option: option name
  150. *
  151. * This function parses a string containing a comma-separated list of
  152. * strings like a=b,c.
  153. *
  154. * Return true if there's such option in the string, or return false.
  155. */
  156. bool parse_option_str(const char *str, const char *option)
  157. {
  158. while (*str) {
  159. if (!strncmp(str, option, strlen(option))) {
  160. str += strlen(option);
  161. if (!*str || *str == ',')
  162. return true;
  163. }
  164. while (*str && *str != ',')
  165. str++;
  166. if (*str == ',')
  167. str++;
  168. }
  169. return false;
  170. }