/*1* linux/lib/cmdline.c2* Helper functions generally used for parsing kernel command line3* and module options.4*5* Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.6*7* This source code is licensed under the GNU General Public License,8* Version 2. See the file COPYING for more details.9*10* GNU Indent formatting options for this file: -kr -i8 -npsl -pcs11*12*/1314#include <linux/module.h>15#include <linux/kernel.h>16#include <linux/string.h>1718/*19* If a hyphen was found in get_option, this will handle the20* range of numbers, M-N. This will expand the range and insert21* the values[M, M+1, ..., N] into the ints array in get_options.22*/2324static int get_range(char **str, int *pint)25{26int x, inc_counter, upper_range;2728(*str)++;29upper_range = simple_strtol((*str), NULL, 0);30inc_counter = upper_range - *pint;31for (x = *pint; x < upper_range; x++)32*pint++ = x;33return inc_counter;34}3536/**37* get_option - Parse integer from an option string38* @str: option string39* @pint: (output) integer value parsed from @str40*41* Read an int from an option string; if available accept a subsequent42* comma as well.43*44* Return values:45* 0 - no int in string46* 1 - int found, no subsequent comma47* 2 - int found including a subsequent comma48* 3 - hyphen found to denote a range49*/5051int get_option (char **str, int *pint)52{53char *cur = *str;5455if (!cur || !(*cur))56return 0;57*pint = simple_strtol (cur, str, 0);58if (cur == *str)59return 0;60if (**str == ',') {61(*str)++;62return 2;63}64if (**str == '-')65return 3;6667return 1;68}6970/**71* get_options - Parse a string into a list of integers72* @str: String to be parsed73* @nints: size of integer array74* @ints: integer array75*76* This function parses a string containing a comma-separated77* list of integers, a hyphen-separated range of _positive_ integers,78* or a combination of both. The parse halts when the array is79* full, or when no more numbers can be retrieved from the80* string.81*82* Return value is the character in the string which caused83* the parse to end (typically a null terminator, if @str is84* completely parseable).85*/8687char *get_options(const char *str, int nints, int *ints)88{89int res, i = 1;9091while (i < nints) {92res = get_option ((char **)&str, ints + i);93if (res == 0)94break;95if (res == 3) {96int range_nums;97range_nums = get_range((char **)&str, ints + i);98if (range_nums < 0)99break;100/*101* Decrement the result by one to leave out the102* last number in the range. The next iteration103* will handle the upper number in the range104*/105i += (range_nums - 1);106}107i++;108if (res == 1)109break;110}111ints[0] = i - 1;112return (char *)str;113}114115/**116* memparse - parse a string with mem suffixes into a number117* @ptr: Where parse begins118* @retptr: (output) Optional pointer to next char after parse completes119*120* Parses a string into a number. The number stored at @ptr is121* potentially suffixed with %K (for kilobytes, or 1024 bytes),122* %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or123* 1073741824). If the number is suffixed with K, M, or G, then124* the return value is the number multiplied by one kilobyte, one125* megabyte, or one gigabyte, respectively.126*/127128unsigned long long memparse(const char *ptr, char **retptr)129{130char *endptr; /* local pointer to end of parsed string */131132unsigned long long ret = simple_strtoull(ptr, &endptr, 0);133134switch (*endptr) {135case 'G':136case 'g':137ret <<= 10;138case 'M':139case 'm':140ret <<= 10;141case 'K':142case 'k':143ret <<= 10;144endptr++;145default:146break;147}148149if (retptr)150*retptr = endptr;151152return ret;153}154155156EXPORT_SYMBOL(memparse);157EXPORT_SYMBOL(get_option);158EXPORT_SYMBOL(get_options);159160161