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
699a968faSDavid Hunt #include <string.h>
799a968faSDavid Hunt #include <rte_log.h>
899a968faSDavid Hunt #include "parse.h"
999a968faSDavid Hunt
1099a968faSDavid Hunt /*
1199a968faSDavid Hunt * Parse elem, the elem could be single number/range or group
1299a968faSDavid Hunt * 1) A single number elem, it's just a simple digit. e.g. 9
1399a968faSDavid Hunt * 2) A single range elem, two digits with a '-' between. e.g. 2-6
1499a968faSDavid Hunt * 3) A group elem, combines multiple 1) or 2) e.g 0,2-4,6
1599a968faSDavid Hunt * Within group, '-' used for a range separator;
1699a968faSDavid Hunt * ',' used for a single number.
1799a968faSDavid Hunt */
1899a968faSDavid Hunt int
parse_set(const char * input,uint16_t set[],unsigned int num)1999a968faSDavid Hunt parse_set(const char *input, uint16_t set[], unsigned int num)
2099a968faSDavid Hunt {
2199a968faSDavid Hunt unsigned int idx;
2299a968faSDavid Hunt const char *str = input;
2399a968faSDavid Hunt char *end = NULL;
2499a968faSDavid Hunt unsigned int min, max;
2599a968faSDavid Hunt
2699a968faSDavid Hunt memset(set, 0, num * sizeof(uint16_t));
2799a968faSDavid Hunt
2899a968faSDavid Hunt while (isblank(*str))
2999a968faSDavid Hunt str++;
3099a968faSDavid Hunt
3199a968faSDavid Hunt /* only digit or left bracket is qualify for start point */
3299a968faSDavid Hunt if (!isdigit(*str) || *str == '\0')
3399a968faSDavid Hunt return -1;
3499a968faSDavid Hunt
3599a968faSDavid Hunt while (isblank(*str))
3699a968faSDavid Hunt str++;
3799a968faSDavid Hunt if (*str == '\0')
3899a968faSDavid Hunt return -1;
3999a968faSDavid Hunt
4099a968faSDavid Hunt min = num;
4199a968faSDavid Hunt do {
4299a968faSDavid Hunt
4399a968faSDavid Hunt /* go ahead to the first digit */
4499a968faSDavid Hunt while (isblank(*str))
4599a968faSDavid Hunt str++;
4699a968faSDavid Hunt if (!isdigit(*str))
4799a968faSDavid Hunt return -1;
4899a968faSDavid Hunt
4999a968faSDavid Hunt /* get the digit value */
5099a968faSDavid Hunt errno = 0;
5199a968faSDavid Hunt idx = strtoul(str, &end, 10);
5299a968faSDavid Hunt if (errno || end == NULL || idx >= num)
5399a968faSDavid Hunt return -1;
5499a968faSDavid Hunt
5599a968faSDavid Hunt /* go ahead to separator '-' and ',' */
5699a968faSDavid Hunt while (isblank(*end))
5799a968faSDavid Hunt end++;
5899a968faSDavid Hunt if (*end == '-') {
5999a968faSDavid Hunt if (min == num)
6099a968faSDavid Hunt min = idx;
6199a968faSDavid Hunt else /* avoid continuous '-' */
6299a968faSDavid Hunt return -1;
63*95f648ffSRory Sexton } else if ((*end == ',') || (*end == ':') || (*end == '\0')) {
6499a968faSDavid Hunt max = idx;
6599a968faSDavid Hunt
6699a968faSDavid Hunt if (min == num)
6799a968faSDavid Hunt min = idx;
6899a968faSDavid Hunt
6999a968faSDavid Hunt for (idx = RTE_MIN(min, max);
7099a968faSDavid Hunt idx <= RTE_MAX(min, max); idx++) {
7199a968faSDavid Hunt set[idx] = 1;
7299a968faSDavid Hunt }
7399a968faSDavid Hunt min = num;
7499a968faSDavid Hunt } else
7599a968faSDavid Hunt return -1;
7699a968faSDavid Hunt
7799a968faSDavid Hunt str = end + 1;
78*95f648ffSRory Sexton } while ((*end != '\0') && (*end != ':'));
79*95f648ffSRory Sexton
80*95f648ffSRory Sexton return str - input;
81*95f648ffSRory Sexton }
82*95f648ffSRory Sexton
83*95f648ffSRory Sexton int
parse_branch_ratio(const char * input,float * branch_ratio)84*95f648ffSRory Sexton parse_branch_ratio(const char *input, float *branch_ratio)
85*95f648ffSRory Sexton {
86*95f648ffSRory Sexton const char *str = input;
87*95f648ffSRory Sexton char *end = NULL;
88*95f648ffSRory Sexton
89*95f648ffSRory Sexton while (isblank(*str))
90*95f648ffSRory Sexton str++;
91*95f648ffSRory Sexton
92*95f648ffSRory Sexton if (*str == '\0')
93*95f648ffSRory Sexton return -1;
94*95f648ffSRory Sexton
95*95f648ffSRory Sexton /* Go straight to the ':' separator if present */
96*95f648ffSRory Sexton while ((*str != '\0') && (*str != ':'))
97*95f648ffSRory Sexton str++;
98*95f648ffSRory Sexton
99*95f648ffSRory Sexton /* Branch ratio not specified in args so leave it at default setting */
100*95f648ffSRory Sexton if (*str == '\0')
101*95f648ffSRory Sexton return 0;
102*95f648ffSRory Sexton
103*95f648ffSRory Sexton /* Confirm ':' separator present */
104*95f648ffSRory Sexton if (*str != ':')
105*95f648ffSRory Sexton return -1;
106*95f648ffSRory Sexton
107*95f648ffSRory Sexton str++;
108*95f648ffSRory Sexton errno = 0;
109*95f648ffSRory Sexton *branch_ratio = strtof(str, &end);
110*95f648ffSRory Sexton if (errno || end == NULL)
111*95f648ffSRory Sexton return -1;
112*95f648ffSRory Sexton
113*95f648ffSRory Sexton if (*end != '\0')
114*95f648ffSRory Sexton return -1;
115*95f648ffSRory Sexton
116*95f648ffSRory Sexton str = end + 1;
11799a968faSDavid Hunt
11899a968faSDavid Hunt return str - input;
11999a968faSDavid Hunt }
120