update Intel copyright years to 2014
[dpdk.git] / lib / librte_cmdline / cmdline_parse_num.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36  * All rights reserved.
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions are met:
39  *
40  *     * Redistributions of source code must retain the above copyright
41  *       notice, this list of conditions and the following disclaimer.
42  *     * Redistributions in binary form must reproduce the above copyright
43  *       notice, this list of conditions and the following disclaimer in the
44  *       documentation and/or other materials provided with the distribution.
45  *     * Neither the name of the University of California, Berkeley nor the
46  *       names of its contributors may be used to endorse or promote products
47  *       derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60
61 #include <stdio.h>
62 #include <stdint.h>
63 #include <inttypes.h>
64 #include <ctype.h>
65 #include <string.h>
66 #include <stdarg.h>
67 #include <errno.h>
68 #include <rte_string_fns.h>
69
70 #include "cmdline_parse.h"
71 #include "cmdline_parse_num.h"
72
73 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
74 #define debug_printf(args...) printf(args)
75 #else
76 #define debug_printf(args...) do {} while(0)
77 #endif
78
79 struct cmdline_token_ops cmdline_token_num_ops = {
80         .parse = cmdline_parse_num,
81         .complete_get_nb = NULL,
82         .complete_get_elt = NULL,
83         .get_help = cmdline_get_help_num,
84 };
85
86
87 enum num_parse_state_t {
88         START,
89         DEC_NEG,
90         BIN,
91         HEX,
92
93         ERROR,
94
95         FIRST_OK, /* not used */
96         ZERO_OK,
97         HEX_OK,
98         OCTAL_OK,
99         BIN_OK,
100         DEC_NEG_OK,
101         DEC_POS_OK,
102 };
103
104 /* Keep it sync with enum in .h */
105 static const char * num_help[] = {
106         "UINT8", "UINT16", "UINT32", "UINT64",
107         "INT8", "INT16", "INT32", "INT64",
108 };
109
110 static inline int
111 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
112 {
113         /* overflow */
114         if ( (UINT64_MAX - c) / base < *res ) {
115                 return -1;
116         }
117
118         *res = (uint64_t) (*res * base + c);
119         return 0;
120 }
121
122
123 /* parse an int */
124 int
125 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
126 {
127         struct cmdline_token_num_data nd;
128         enum num_parse_state_t st = START;
129         const char * buf;
130         char c;
131         uint64_t res1 = 0;
132
133         if (!tk)
134                 return -1;
135
136         if (!srcbuf || !*srcbuf)
137                 return -1;
138
139         buf = srcbuf;
140         c = *buf;
141
142         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
143
144         while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
145                 debug_printf("%c %x -> ", c, c);
146                 switch (st) {
147                 case START:
148                         if (c == '-') {
149                                 st = DEC_NEG;
150                         }
151                         else if (c == '0') {
152                                 st = ZERO_OK;
153                         }
154                         else if (c >= '1' && c <= '9') {
155                                 if (add_to_res(c - '0', &res1, 10) < 0)
156                                         st = ERROR;
157                                 else
158                                         st = DEC_POS_OK;
159                         }
160                         else  {
161                                 st = ERROR;
162                         }
163                         break;
164
165                 case ZERO_OK:
166                         if (c == 'x') {
167                                 st = HEX;
168                         }
169                         else if (c == 'b') {
170                                 st = BIN;
171                         }
172                         else if (c >= '0' && c <= '7') {
173                                 if (add_to_res(c - '0', &res1, 10) < 0)
174                                         st = ERROR;
175                                 else
176                                         st = OCTAL_OK;
177                         }
178                         else  {
179                                 st = ERROR;
180                         }
181                         break;
182
183                 case DEC_NEG:
184                         if (c >= '0' && c <= '9') {
185                                 if (add_to_res(c - '0', &res1, 10) < 0)
186                                         st = ERROR;
187                                 else
188                                         st = DEC_NEG_OK;
189                         }
190                         else {
191                                 st = ERROR;
192                         }
193                         break;
194
195                 case DEC_NEG_OK:
196                         if (c >= '0' && c <= '9') {
197                                 if (add_to_res(c - '0', &res1, 10) < 0)
198                                         st = ERROR;
199                         }
200                         else {
201                                 st = ERROR;
202                         }
203                         break;
204
205                 case DEC_POS_OK:
206                         if (c >= '0' && c <= '9') {
207                                 if (add_to_res(c - '0', &res1, 10) < 0)
208                                         st = ERROR;
209                         }
210                         else {
211                                 st = ERROR;
212                         }
213                         break;
214
215                 case HEX:
216                         st = HEX_OK;
217                         /* no break */
218                 case HEX_OK:
219                         if (c >= '0' && c <= '9') {
220                                 if (add_to_res(c - '0', &res1, 16) < 0)
221                                         st = ERROR;
222                         }
223                         else if (c >= 'a' && c <= 'f') {
224                                 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
225                                         st = ERROR;
226                         }
227                         else if (c >= 'A' && c <= 'F') {
228                                 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
229                                         st = ERROR;
230                         }
231                         else {
232                                 st = ERROR;
233                         }
234                         break;
235
236
237                 case OCTAL_OK:
238                         if (c >= '0' && c <= '7') {
239                                 if (add_to_res(c - '0', &res1, 8) < 0)
240                                         st = ERROR;
241                         }
242                         else {
243                                 st = ERROR;
244                         }
245                         break;
246
247                 case BIN:
248                         st = BIN_OK;
249                         /* no break */
250                 case BIN_OK:
251                         if (c >= '0' && c <= '1') {
252                                 if (add_to_res(c - '0', &res1, 2) < 0)
253                                         st = ERROR;
254                         }
255                         else {
256                                 st = ERROR;
257                         }
258                         break;
259                 default:
260                         debug_printf("not impl ");
261
262                 }
263
264                 debug_printf("(%"PRIu64")\n", res1);
265
266                 buf ++;
267                 c = *buf;
268
269                 /* token too long */
270                 if (buf-srcbuf > 127)
271                         return -1;
272         }
273
274         switch (st) {
275         case ZERO_OK:
276         case DEC_POS_OK:
277         case HEX_OK:
278         case OCTAL_OK:
279         case BIN_OK:
280                 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
281                         if (res) *(int8_t *)res = (int8_t) res1;
282                         return (buf-srcbuf);
283                 }
284                 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
285                         if (res) *(int16_t *)res = (int16_t) res1;
286                         return (buf-srcbuf);
287                 }
288                 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
289                         if (res) *(int32_t *)res = (int32_t) res1;
290                         return (buf-srcbuf);
291                 }
292                 else if ( nd.type == INT64 && res1 <= INT64_MAX ) {
293                         if (res) *(int64_t *)res = (int64_t) res1;
294                         return (buf-srcbuf);
295                 }
296                 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
297                         if (res) *(uint8_t *)res = (uint8_t) res1;
298                         return (buf-srcbuf);
299                 }
300                 else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
301                         if (res) *(uint16_t *)res = (uint16_t) res1;
302                         return (buf-srcbuf);
303                 }
304                 else if ( nd.type == UINT32 && res1 <= UINT32_MAX ) {
305                         if (res) *(uint32_t *)res = (uint32_t) res1;
306                         return (buf-srcbuf);
307                 }
308                 else if ( nd.type == UINT64 ) {
309                         if (res) *(uint64_t *)res = res1;
310                         return (buf-srcbuf);
311                 }
312                 else {
313                         return -1;
314                 }
315                 break;
316
317         case DEC_NEG_OK:
318                 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
319                         if (res) *(int8_t *)res = (int8_t) (-res1);
320                         return (buf-srcbuf);
321                 }
322                 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
323                         if (res) *(int16_t *)res = (int16_t) (-res1);
324                         return (buf-srcbuf);
325                 }
326                 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
327                         if (res) *(int32_t *)res = (int32_t) (-res1);
328                         return (buf-srcbuf);
329                 }
330                 else if ( nd.type == INT64 && res1 <= (uint64_t)INT64_MAX + 1 ) {
331                         if (res) *(int64_t *)res = (int64_t) (-res1);
332                         return (buf-srcbuf);
333                 }
334                 else {
335                         return -1;
336                 }
337                 break;
338         default:
339                 debug_printf("error\n");
340                 return -1;
341         }
342 }
343
344
345 /* parse an int */
346 int
347 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
348 {
349         struct cmdline_token_num_data nd;
350         int ret;
351
352         if (!tk)
353                 return -1;
354
355         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
356
357         /* should not happen.... don't so this test */
358         /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
359         /* return -1; */
360
361         ret = rte_snprintf(dstbuf, size, "%s", num_help[nd.type]);
362         if (ret < 0)
363                 return -1;
364         dstbuf[size-1] = '\0';
365         return 0;
366 }