cmdline.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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)
  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; x < upper_range; x++)
  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);
  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 (for kilobytes, or 1024 bytes),
  113. * %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
  114. * 1073741824). If the number is suffixed with K, M, or G, then
  115. * the return value is the number multiplied by one kilobyte, one
  116. * megabyte, or one gigabyte, respectively.
  117. */
  118. unsigned long long memparse(const char *ptr, char **retptr)
  119. {
  120. char *endptr; /* local pointer to end of parsed string */
  121. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  122. switch (*endptr) {
  123. case 'G':
  124. case 'g':
  125. ret <<= 10;
  126. case 'M':
  127. case 'm':
  128. ret <<= 10;
  129. case 'K':
  130. case 'k':
  131. ret <<= 10;
  132. endptr++;
  133. default:
  134. break;
  135. }
  136. if (retptr)
  137. *retptr = endptr;
  138. return ret;
  139. }
  140. EXPORT_SYMBOL(memparse);