199a968faSDavid Hunt /* SPDX-License-Identifier: BSD-3-Clause
299a968faSDavid Hunt * Copyright(c) 2010-2014 Intel Corporation.
399a968faSDavid Hunt * Copyright(c) 2014 6WIND S.A.
499a968faSDavid Hunt */
599a968faSDavid Hunt
6*72b452c5SDmitry Kozlyuk #include <ctype.h>
7*72b452c5SDmitry Kozlyuk #include <errno.h>
8*72b452c5SDmitry Kozlyuk #include <stdlib.h>
999a968faSDavid Hunt #include <string.h>
10*72b452c5SDmitry Kozlyuk
1199a968faSDavid Hunt #include <rte_log.h>
12*72b452c5SDmitry Kozlyuk
1399a968faSDavid Hunt #include "parse.h"
1499a968faSDavid Hunt
1599a968faSDavid Hunt /*
1699a968faSDavid Hunt * Parse elem, the elem could be single number/range or group
1799a968faSDavid Hunt * 1) A single number elem, it's just a simple digit. e.g. 9
1899a968faSDavid Hunt * 2) A single range elem, two digits with a '-' between. e.g. 2-6
1999a968faSDavid Hunt * 3) A group elem, combines multiple 1) or 2) e.g 0,2-4,6
2099a968faSDavid Hunt * Within group, '-' used for a range separator;
2199a968faSDavid Hunt * ',' used for a single number.
2299a968faSDavid Hunt */
2399a968faSDavid Hunt int
parse_set(const char * input,uint16_t set[],unsigned int num)2499a968faSDavid Hunt parse_set(const char *input, uint16_t set[], unsigned int num)
2599a968faSDavid Hunt {
2699a968faSDavid Hunt unsigned int idx;
2799a968faSDavid Hunt const char *str = input;
2899a968faSDavid Hunt char *end = NULL;
2999a968faSDavid Hunt unsigned int min, max;
3099a968faSDavid Hunt
3199a968faSDavid Hunt memset(set, 0, num * sizeof(uint16_t));
3299a968faSDavid Hunt
3399a968faSDavid Hunt while (isblank(*str))
3499a968faSDavid Hunt str++;
3599a968faSDavid Hunt
3699a968faSDavid Hunt /* only digit or left bracket is qualify for start point */
3799a968faSDavid Hunt if (!isdigit(*str) || *str == '\0')
3899a968faSDavid Hunt return -1;
3999a968faSDavid Hunt
4099a968faSDavid Hunt while (isblank(*str))
4199a968faSDavid Hunt str++;
4299a968faSDavid Hunt if (*str == '\0')
4399a968faSDavid Hunt return -1;
4499a968faSDavid Hunt
4599a968faSDavid Hunt min = num;
4699a968faSDavid Hunt do {
4799a968faSDavid Hunt
4899a968faSDavid Hunt /* go ahead to the first digit */
4999a968faSDavid Hunt while (isblank(*str))
5099a968faSDavid Hunt str++;
5199a968faSDavid Hunt if (!isdigit(*str))
5299a968faSDavid Hunt return -1;
5399a968faSDavid Hunt
5499a968faSDavid Hunt /* get the digit value */
5599a968faSDavid Hunt errno = 0;
5699a968faSDavid Hunt idx = strtoul(str, &end, 10);
5799a968faSDavid Hunt if (errno || end == NULL || idx >= num)
5899a968faSDavid Hunt return -1;
5999a968faSDavid Hunt
6099a968faSDavid Hunt /* go ahead to separator '-' and ',' */
6199a968faSDavid Hunt while (isblank(*end))
6299a968faSDavid Hunt end++;
6399a968faSDavid Hunt if (*end == '-') {
6499a968faSDavid Hunt if (min == num)
6599a968faSDavid Hunt min = idx;
6699a968faSDavid Hunt else /* avoid continuous '-' */
6799a968faSDavid Hunt return -1;
6895f648ffSRory Sexton } else if ((*end == ',') || (*end == ':') || (*end == '\0')) {
6999a968faSDavid Hunt max = idx;
7099a968faSDavid Hunt
7199a968faSDavid Hunt if (min == num)
7299a968faSDavid Hunt min = idx;
7399a968faSDavid Hunt
7499a968faSDavid Hunt for (idx = RTE_MIN(min, max);
7599a968faSDavid Hunt idx <= RTE_MAX(min, max); idx++) {
7699a968faSDavid Hunt set[idx] = 1;
7799a968faSDavid Hunt }
7899a968faSDavid Hunt min = num;
7999a968faSDavid Hunt } else
8099a968faSDavid Hunt return -1;
8199a968faSDavid Hunt
8299a968faSDavid Hunt str = end + 1;
8395f648ffSRory Sexton } while ((*end != '\0') && (*end != ':'));
8495f648ffSRory Sexton
8595f648ffSRory Sexton return str - input;
8695f648ffSRory Sexton }
8795f648ffSRory Sexton
8895f648ffSRory Sexton int
parse_branch_ratio(const char * input,float * branch_ratio)8995f648ffSRory Sexton parse_branch_ratio(const char *input, float *branch_ratio)
9095f648ffSRory Sexton {
9195f648ffSRory Sexton const char *str = input;
9295f648ffSRory Sexton char *end = NULL;
9395f648ffSRory Sexton
9495f648ffSRory Sexton while (isblank(*str))
9595f648ffSRory Sexton str++;
9695f648ffSRory Sexton
9795f648ffSRory Sexton if (*str == '\0')
9895f648ffSRory Sexton return -1;
9995f648ffSRory Sexton
10095f648ffSRory Sexton /* Go straight to the ':' separator if present */
10195f648ffSRory Sexton while ((*str != '\0') && (*str != ':'))
10295f648ffSRory Sexton str++;
10395f648ffSRory Sexton
10495f648ffSRory Sexton /* Branch ratio not specified in args so leave it at default setting */
10595f648ffSRory Sexton if (*str == '\0')
10695f648ffSRory Sexton return 0;
10795f648ffSRory Sexton
10895f648ffSRory Sexton /* Confirm ':' separator present */
10995f648ffSRory Sexton if (*str != ':')
11095f648ffSRory Sexton return -1;
11195f648ffSRory Sexton
11295f648ffSRory Sexton str++;
11395f648ffSRory Sexton errno = 0;
11495f648ffSRory Sexton *branch_ratio = strtof(str, &end);
11595f648ffSRory Sexton if (errno || end == NULL)
11695f648ffSRory Sexton return -1;
11795f648ffSRory Sexton
11895f648ffSRory Sexton if (*end != '\0')
11995f648ffSRory Sexton return -1;
12095f648ffSRory Sexton
12195f648ffSRory Sexton str = end + 1;
12299a968faSDavid Hunt
12399a968faSDavid Hunt return str - input;
12499a968faSDavid Hunt }
125