cmdline.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/module.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. /**
  63. * get_options - Parse a string into a list of integers
  64. * @str: String to be parsed
  65. * @nints: size of integer array
  66. * @ints: integer array
  67. *
  68. * This function parses a string containing a comma-separated
  69. * list of integers, a hyphen-separated range of _positive_ integers,
  70. * or a combination of both. The parse halts when the array is
  71. * full, or when no more numbers can be retrieved from the
  72. * string.
  73. *
  74. * Return value is the character in the string which caused
  75. * the parse to end (typically a null terminator, if @str is
  76. * completely parseable).
  77. */
  78. char *get_options(const char *str, int nints, int *ints)
  79. {
  80. int res, i = 1;
  81. while (i < nints) {
  82. res = get_option ((char **)&str, ints + i);
  83. if (res == 0)
  84. break;
  85. if (res == 3) {
  86. int range_nums;
  87. range_nums = get_range((char **)&str, ints + i);
  88. if (range_nums < 0)
  89. break;
  90. /*
  91. * Decrement the result by one to leave out the
  92. * last number in the range. The next iteration
  93. * will handle the upper number in the range
  94. */
  95. i += (range_nums - 1);
  96. }
  97. i++;
  98. if (res == 1)
  99. break;
  100. }
  101. ints[0] = i - 1;
  102. return (char *)str;
  103. }
  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 (for kilobytes, or 1024 bytes),
  111. * %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
  112. * 1073741824). If the number is suffixed with K, M, or G, then
  113. * the return value is the number multiplied by one kilobyte, one
  114. * megabyte, or one gigabyte, respectively.
  115. */
  116. unsigned long long memparse(const char *ptr, char **retptr)
  117. {
  118. char *endptr; /* local pointer to end of parsed string */
  119. unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
  120. switch (*endptr) {
  121. case 'G':
  122. case 'g':
  123. ret <<= 10;
  124. case 'M':
  125. case 'm':
  126. ret <<= 10;
  127. case 'K':
  128. case 'k':
  129. ret <<= 10;
  130. endptr++;
  131. default:
  132. break;
  133. }
  134. if (retptr)
  135. *retptr = endptr;
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(memparse);
  139. EXPORT_SYMBOL(get_option);
  140. EXPORT_SYMBOL(get_options);