8ef2964deebe2c8dca940a27ee5b584312dc8610
[libcmdline.git] / src / lib / cmdline_parse_num.c
1 /*-
2  * Copyright (c) <2010>, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the
15  *   distribution.
16  *
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
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37  * All rights reserved.
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in the
45  *       documentation and/or other materials provided with the distribution.
46  *     * Neither the name of the University of California, Berkeley nor the
47  *       names of its contributors may be used to endorse or promote products
48  *       derived from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61
62 #include <stdio.h>
63 #include <stdint.h>
64 #include <inttypes.h>
65 #include <ctype.h>
66 #include <string.h>
67
68 #include "cmdline_parse.h"
69 #include "cmdline_parse_num.h"
70
71 //#define debug_printf(args...) printf(args)
72 #define debug_printf(args...) do {} while(0)
73
74 struct cmdline_token_ops cmdline_token_num_ops = {
75         .parse = cmdline_parse_num,
76         .complete_get_nb = NULL,
77         .complete_get_elt = NULL,
78         .get_help = cmdline_get_help_num,
79 };
80
81
82 enum num_parse_state_t {
83         START,
84         DEC_NEG,
85         BIN,
86         HEX,
87         FLOAT_POS,
88         FLOAT_NEG,
89
90         ERROR,
91
92         FIRST_OK, /* not used */
93         ZERO_OK,
94         HEX_OK,
95         OCTAL_OK,
96         BIN_OK,
97         DEC_NEG_OK,
98         DEC_POS_OK,
99         FLOAT_POS_OK,
100         FLOAT_NEG_OK
101 };
102
103 /* Keep it sync with enum in .h */
104 static const char * num_help[] = {
105         "UINT8", "UINT16", "UINT32", "UINT64",
106         "INT8", "INT16", "INT32", "INT64",
107 #ifdef CMDLINE_HAVE_FLOAT
108         "FLOAT",
109 #endif
110 };
111
112 static inline int
113 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
114 {
115         /* overflow */
116         if ( (UINT64_MAX - c) / base < *res ) {
117                 return -1;
118         }
119
120         *res = (uint64_t) (*res * base + c);
121         return 0;
122 }
123
124 static int check_res_size(struct cmdline_token_num_data *nd, unsigned ressize)
125 {
126         switch (nd->type) {
127                 case INT8:
128                 case UINT8:
129                         if (ressize < sizeof(int8_t))
130                                 return -1;
131                         break;
132                 case INT16:
133                 case UINT16:
134                         if (ressize < sizeof(int16_t))
135                                 return -1;
136                         break;
137                 case INT32:
138                 case UINT32:
139                         if (ressize < sizeof(int32_t))
140                                 return -1;
141                         break;
142                 case INT64:
143                 case UINT64:
144                         if (ressize < sizeof(int64_t))
145                                 return -1;
146                         break;
147 #ifdef CMDLINE_HAVE_FLOAT
148                 case FLOAT:
149                         if (ressize < sizeof(float))
150                                 return -1;
151                         break;
152 #endif
153                 default:
154                         return -1;
155         }
156         return 0;
157 }
158
159 /* parse an int or a float */
160 int
161 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
162                   void *res, unsigned ressize)
163 {
164         struct cmdline_token_num_data nd;
165         enum num_parse_state_t st = START;
166         const char * buf = srcbuf;
167         char c = *buf;
168         uint64_t res1 = 0;
169 #ifdef CMDLINE_HAVE_FLOAT
170         uint64_t res2 = 0, res3 = 1;
171 #endif
172
173         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
174
175         /* check that we have enough room in res */
176         if (res) {
177                 if (check_res_size(&nd, ressize) < 0)
178                         return -1;
179         }
180
181         while (st != ERROR && c != '\0') {
182                 debug_printf("%c %x -> ", c, c);
183                 switch (st) {
184                 case START:
185                         if (c == '-') {
186                                 st = DEC_NEG;
187                         }
188                         else if (c == '0') {
189                                 st = ZERO_OK;
190                         }
191 #ifdef CMDLINE_HAVE_FLOAT
192                         else if (c == '.') {
193                                 st = FLOAT_POS;
194                                 res1 = 0;
195                         }
196 #endif
197                         else if (c >= '1' && c <= '9') {
198                                 if (add_to_res(c - '0', &res1, 10) < 0)
199                                         st = ERROR;
200                                 else
201                                         st = DEC_POS_OK;
202                         }
203                         else  {
204                                 st = ERROR;
205                         }
206                         break;
207
208                 case ZERO_OK:
209                         if (c == 'x') {
210                                 st = HEX;
211                         }
212                         else if (c == 'b') {
213                                 st = BIN;
214                         }
215 #ifdef CMDLINE_HAVE_FLOAT
216                         else if (c == '.') {
217                                 st = FLOAT_POS;
218                                 res1 = 0;
219                         }
220 #endif
221                         else if (c >= '0' && c <= '7') {
222                                 if (add_to_res(c - '0', &res1, 10) < 0)
223                                         st = ERROR;
224                                 else
225                                         st = OCTAL_OK;
226                         }
227                         else  {
228                                 st = ERROR;
229                         }
230                         break;
231
232                 case DEC_NEG:
233                         if (c >= '0' && c <= '9') {
234                                 if (add_to_res(c - '0', &res1, 10) < 0)
235                                         st = ERROR;
236                                 else
237                                         st = DEC_NEG_OK;
238                         }
239 #ifdef CMDLINE_HAVE_FLOAT
240                         else if (c == '.') {
241                                 res1 = 0;
242                                 st = FLOAT_NEG;
243                         }
244 #endif
245                         else {
246                                 st = ERROR;
247                         }
248                         break;
249
250                 case DEC_NEG_OK:
251                         if (c >= '0' && c <= '9') {
252                                 if (add_to_res(c - '0', &res1, 10) < 0)
253                                         st = ERROR;
254                         }
255 #ifdef CMDLINE_HAVE_FLOAT
256                         else if (c == '.') {
257                                 st = FLOAT_NEG;
258                         }
259 #endif
260                         else {
261                                 st = ERROR;
262                         }
263                         break;
264
265                 case DEC_POS_OK:
266                         if (c >= '0' && c <= '9') {
267                                 if (add_to_res(c - '0', &res1, 10) < 0)
268                                         st = ERROR;
269                         }
270 #ifdef CMDLINE_HAVE_FLOAT
271                         else if (c == '.') {
272                                 st = FLOAT_POS;
273                         }
274 #endif
275                         else {
276                                 st = ERROR;
277                         }
278                         break;
279
280                 case HEX:
281                         st = HEX_OK;
282                         /* no break */
283                 case HEX_OK:
284                         if (c >= '0' && c <= '9') {
285                                 if (add_to_res(c - '0', &res1, 16) < 0)
286                                         st = ERROR;
287                         }
288                         else if (c >= 'a' && c <= 'f') {
289                                 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
290                                         st = ERROR;
291                         }
292                         else if (c >= 'A' && c <= 'F') {
293                                 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
294                                         st = ERROR;
295                         }
296                         else {
297                                 st = ERROR;
298                         }
299                         break;
300
301
302                 case OCTAL_OK:
303                         if (c >= '0' && c <= '7') {
304                                 if (add_to_res(c - '0', &res1, 8) < 0)
305                                         st = ERROR;
306                         }
307                         else {
308                                 st = ERROR;
309                         }
310                         break;
311
312                 case BIN:
313                         st = BIN_OK;
314                         /* no break */
315                 case BIN_OK:
316                         if (c >= '0' && c <= '1') {
317                                 if (add_to_res(c - '0', &res1, 2) < 0)
318                                         st = ERROR;
319                         }
320                         else {
321                                 st = ERROR;
322                         }
323                         break;
324
325 #ifdef CMDLINE_HAVE_FLOAT
326                 case FLOAT_POS:
327                         if (c >= '0' && c <= '9') {
328                                 if (add_to_res(c - '0', &res2, 10) < 0)
329                                         st = ERROR;
330                                 else
331                                         st = FLOAT_POS_OK;
332                                 res3 = 10;
333                         }
334                         else {
335                                 st = ERROR;
336                         }
337                         break;
338
339                 case FLOAT_NEG:
340                         if (c >= '0' && c <= '9') {
341                                 if (add_to_res(c - '0', &res2, 10) < 0)
342                                         st = ERROR;
343                                 else
344                                         st = FLOAT_NEG_OK;
345                                 res3 = 10;
346                         }
347                         else {
348                                 st = ERROR;
349                         }
350                         break;
351
352                 case FLOAT_POS_OK:
353                         if (c >= '0' && c <= '9') {
354                                 if (add_to_res(c - '0', &res2, 10) < 0)
355                                         st = ERROR;
356                                 if (add_to_res(0, &res3, 10) < 0)
357                                         st = ERROR;
358                         }
359                         else {
360                                 st = ERROR;
361                         }
362                         break;
363
364                 case FLOAT_NEG_OK:
365                         if (c >= '0' && c <= '9') {
366                                 if (add_to_res(c - '0', &res2, 10) < 0)
367                                         st = ERROR;
368                                 if (add_to_res(0, &res3, 10) < 0)
369                                         st = ERROR;
370                         }
371                         else {
372                                 st = ERROR;
373                         }
374                         break;
375 #endif
376
377                 default:
378                         debug_printf("not impl ");
379
380                 }
381
382 #ifdef CMDLINE_HAVE_FLOAT
383                 debug_printf("(%"PRIu32")  (%"PRIu32")  (%"PRIu32")\n",
384                              res1, res2, res3);
385 #else
386                 debug_printf("(%"PRIu32")\n", res1);
387 #endif
388
389                 buf ++;
390                 c = *buf;
391
392                 /* token too long */
393                 if (buf-srcbuf > 127)
394                         return -1;
395         }
396
397         switch (st) {
398         case ZERO_OK:
399         case DEC_POS_OK:
400         case HEX_OK:
401         case OCTAL_OK:
402         case BIN_OK:
403                 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
404                         if (res)
405                                 *(int8_t *)res = (int8_t) res1;
406                         return (buf-srcbuf);
407                 }
408                 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
409                         if (res)
410                                 *(int16_t *)res = (int16_t) res1;
411                         return (buf-srcbuf);
412                 }
413                 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
414                         if (res)
415                                 *(int32_t *)res = (int32_t) res1;
416                         return (buf-srcbuf);
417                 }
418                 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
419                         if (res)
420                                 *(uint8_t *)res = (uint8_t) res1;
421                         return (buf-srcbuf);
422                 }
423                 else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
424                         if (res)
425                                 *(uint16_t *)res = (uint16_t) res1;
426                         return (buf-srcbuf);
427                 }
428                 else if ( nd.type == UINT32 ) {
429                         if (res)
430                                 *(uint32_t *)res = (uint32_t) res1;
431                         return (buf-srcbuf);
432                 }
433                 else if ( nd.type == UINT64 ) {
434                         if (res)
435                                 *(uint64_t *)res = res1;
436                         return (buf-srcbuf);
437                 }
438 #ifdef CMDLINE_HAVE_FLOAT
439                 else if ( nd.type == FLOAT ) {
440                         if (res)
441                                 *(float *)res = (float)res1;
442                         return (buf-srcbuf);
443                 }
444 #endif
445                 else {
446                         return -1;
447                 }
448                 break;
449
450         case DEC_NEG_OK:
451                 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
452                         if (res)
453                                 *(int8_t *)res = (int8_t) (-res1);
454                         return (buf-srcbuf);
455                 }
456                 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
457                         if (res)
458                                 *(int16_t *)res = (int16_t) (-res1);
459                         return (buf-srcbuf);
460                 }
461                 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
462                         if (res)
463                                 *(int32_t *)res = (int32_t) (-res1);
464                         return (buf-srcbuf);
465                 }
466 #ifdef CMDLINE_HAVE_FLOAT
467                 else if ( nd.type == FLOAT ) {
468                         if (res)
469                                 *(float *)res = - (float)res1;
470                         return (buf-srcbuf);
471                 }
472 #endif
473                 else {
474                         return -1;
475                 }
476                 break;
477
478 #ifdef CMDLINE_HAVE_FLOAT
479         case FLOAT_POS:
480         case FLOAT_POS_OK:
481                 if ( nd.type == FLOAT ) {
482                         if (res)
483                                 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
484                         return (buf-srcbuf);
485
486                 }
487                 else {
488                         return -1;
489                 }
490                 break;
491
492         case FLOAT_NEG:
493         case FLOAT_NEG_OK:
494                 if ( nd.type == FLOAT ) {
495                         if (res)
496                                 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
497                         return (buf-srcbuf);
498
499                 }
500                 else {
501                         return -1;
502                 }
503                 break;
504 #endif
505         default:
506                 debug_printf("error\n");
507                 return -1;
508         }
509 }
510
511
512 /* parse an int or a float */
513 int
514 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
515 {
516         struct cmdline_token_num_data nd;
517
518         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
519
520         /* should not happen.... don't so this test */
521         /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
522         /* return -1; */
523
524         strncpy(dstbuf, num_help[nd.type], size);
525         dstbuf[size-1] = '\0';
526         return 0;
527 }