cmdline: use SPDX tags
[dpdk.git] / lib / librte_cmdline / cmdline_parse_portlist.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright (c) 2010, Keith Wiles <keith.wiles@windriver.com>
4  * All rights reserved.
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <inttypes.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <stdarg.h>
14
15 #include <rte_string_fns.h>
16 #include "cmdline_parse.h"
17 #include "cmdline_parse_portlist.h"
18
19 struct cmdline_token_ops cmdline_token_portlist_ops = {
20         .parse = cmdline_parse_portlist,
21         .complete_get_nb = NULL,
22         .complete_get_elt = NULL,
23         .get_help = cmdline_get_help_portlist,
24 };
25
26 static void
27 parse_set_list(cmdline_portlist_t *pl, size_t low, size_t high)
28 {
29         do {
30                 pl->map |= (1 << low++);
31         } while (low <= high);
32 }
33
34 static int
35 parse_ports(cmdline_portlist_t *pl, const char *str)
36 {
37         size_t ps, pe;
38         const char *first, *last;
39         char *end;
40
41         for (first = str, last = first;
42             first != NULL && last != NULL;
43             first = last + 1) {
44
45                 last = strchr(first, ',');
46
47                 errno = 0;
48                 ps = strtoul(first, &end, 10);
49                 if (errno != 0 || end == first ||
50                     (end[0] != '-' && end[0] != 0 && end != last))
51                         return -1;
52
53                 /* Support for N-M portlist format */
54                 if (end[0] == '-') {
55                         errno = 0;
56                         first = end + 1;
57                         pe = strtoul(first, &end, 10);
58                         if (errno != 0 || end == first ||
59                             (end[0] != 0 && end != last))
60                                 return -1;
61                 } else {
62                         pe = ps;
63                 }
64
65                 if (ps > pe || pe >= sizeof (pl->map) * 8)
66                         return -1;
67
68                 parse_set_list(pl, ps, pe);
69         }
70
71         return 0;
72 }
73
74 int
75 cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
76         const char *buf, void *res, unsigned ressize)
77 {
78         unsigned int token_len = 0;
79         char portlist_str[PORTLIST_TOKEN_SIZE+1];
80         cmdline_portlist_t *pl;
81
82         if (!buf || ! *buf)
83                 return -1;
84
85         if (res && ressize < sizeof(cmdline_portlist_t))
86                 return -1;
87
88         pl = res;
89
90         while (!cmdline_isendoftoken(buf[token_len]) &&
91             (token_len < PORTLIST_TOKEN_SIZE))
92                 token_len++;
93
94         if (token_len >= PORTLIST_TOKEN_SIZE)
95                 return -1;
96
97         snprintf(portlist_str, token_len+1, "%s", buf);
98
99         if (pl) {
100                 pl->map = 0;
101                 if (strcmp("all", portlist_str) == 0)
102                         pl->map = UINT32_MAX;
103                 else if (parse_ports(pl, portlist_str) != 0)
104                         return -1;
105         }
106
107         return token_len;
108 }
109
110 int
111 cmdline_get_help_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
112                 char *dstbuf, unsigned int size)
113 {
114         int ret;
115         ret = snprintf(dstbuf, size, "range of ports as 3,4-6,8-19,20");
116         if (ret < 0)
117                 return -1;
118         return 0;
119 }