cmdline: big rework and clean of cmdline library
[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 enum num_parse_state_t {
75         START,
76         DEC_NEG,
77         BIN,
78         HEX,
79         FLOAT_POS,
80         FLOAT_NEG,
81
82         ERROR,
83
84         FIRST_OK, /* not used */
85         ZERO_OK,
86         HEX_OK,
87         OCTAL_OK,
88         BIN_OK,
89         DEC_NEG_OK,
90         DEC_POS_OK,
91         FLOAT_POS_OK,
92         FLOAT_NEG_OK
93 };
94
95 /* Keep it sync with enum in .h */
96 static const char * num_help[] = {
97         "UINT8", "UINT16", "UINT32", "UINT64",
98         "INT8", "INT16", "INT32", "INT64",
99 #ifdef CMDLINE_HAVE_FLOAT
100         "FLOAT",
101 #endif
102 };
103
104 static int
105 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
106 {
107         /* overflow */
108         if ( (UINT64_MAX - c) / base < *res ) {
109                 return -1;
110         }
111
112         *res = (uint64_t) (*res * base + c);
113         return 0;
114 }
115
116 static int
117 check_res_size(struct cmdline_token_num_data *nd, unsigned ressize)
118 {
119         switch (nd->type) {
120                 case INT8:
121                 case UINT8:
122                         if (ressize < sizeof(int8_t))
123                                 return -1;
124                         break;
125                 case INT16:
126                 case UINT16:
127                         if (ressize < sizeof(int16_t))
128                                 return -1;
129                         break;
130                 case INT32:
131                 case UINT32:
132                         if (ressize < sizeof(int32_t))
133                                 return -1;
134                         break;
135                 case INT64:
136                 case UINT64:
137                         if (ressize < sizeof(int64_t))
138                                 return -1;
139                         break;
140 #ifdef CMDLINE_HAVE_FLOAT
141                 case FLOAT:
142                         if (ressize < sizeof(float))
143                                 return -1;
144                         break;
145 #endif
146                 default:
147                         return -1;
148         }
149         return 0;
150 }
151
152 /* parse an int or a float */
153 static int
154 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
155                   void *res, unsigned ressize)
156 {
157         struct cmdline_token_num_data nd;
158         enum num_parse_state_t st = START;
159         const char * buf = srcbuf;
160         char c = *buf;
161         uint64_t res1 = 0;
162 #ifdef CMDLINE_HAVE_FLOAT
163         uint64_t res2 = 0, res3 = 1;
164 #endif
165
166         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
167
168         /* check that we have enough room in res */
169         if (res) {
170                 if (check_res_size(&nd, ressize) < 0)
171                         return -1;
172         }
173
174         while (st != ERROR && c != '\0') {
175                 debug_printf("%c %x -> ", c, c);
176                 switch (st) {
177                 case START:
178                         if (c == '-') {
179                                 st = DEC_NEG;
180                         }
181                         else if (c == '0') {
182                                 st = ZERO_OK;
183                         }
184 #ifdef CMDLINE_HAVE_FLOAT
185                         else if (c == '.') {
186                                 st = FLOAT_POS;
187                                 res1 = 0;
188                         }
189 #endif
190                         else if (c >= '1' && c <= '9') {
191                                 if (add_to_res(c - '0', &res1, 10) < 0)
192                                         st = ERROR;
193                                 else
194                                         st = DEC_POS_OK;
195                         }
196                         else  {
197                                 st = ERROR;
198                         }
199                         break;
200
201                 case ZERO_OK:
202                         if (c == 'x') {
203                                 st = HEX;
204                         }
205                         else if (c == 'b') {
206                                 st = BIN;
207                         }
208 #ifdef CMDLINE_HAVE_FLOAT
209                         else if (c == '.') {
210                                 st = FLOAT_POS;
211                                 res1 = 0;
212                         }
213 #endif
214                         else if (c >= '0' && c <= '7') {
215                                 if (add_to_res(c - '0', &res1, 10) < 0)
216                                         st = ERROR;
217                                 else
218                                         st = OCTAL_OK;
219                         }
220                         else  {
221                                 st = ERROR;
222                         }
223                         break;
224
225                 case DEC_NEG:
226                         if (c >= '0' && c <= '9') {
227                                 if (add_to_res(c - '0', &res1, 10) < 0)
228                                         st = ERROR;
229                                 else
230                                         st = DEC_NEG_OK;
231                         }
232 #ifdef CMDLINE_HAVE_FLOAT
233                         else if (c == '.') {
234                                 res1 = 0;
235                                 st = FLOAT_NEG;
236                         }
237 #endif
238                         else {
239                                 st = ERROR;
240                         }
241                         break;
242
243                 case DEC_NEG_OK:
244                         if (c >= '0' && c <= '9') {
245                                 if (add_to_res(c - '0', &res1, 10) < 0)
246                                         st = ERROR;
247                         }
248 #ifdef CMDLINE_HAVE_FLOAT
249                         else if (c == '.') {
250                                 st = FLOAT_NEG;
251                         }
252 #endif
253                         else {
254                                 st = ERROR;
255                         }
256                         break;
257
258                 case DEC_POS_OK:
259                         if (c >= '0' && c <= '9') {
260                                 if (add_to_res(c - '0', &res1, 10) < 0)
261                                         st = ERROR;
262                         }
263 #ifdef CMDLINE_HAVE_FLOAT
264                         else if (c == '.') {
265                                 st = FLOAT_POS;
266                         }
267 #endif
268                         else {
269                                 st = ERROR;
270                         }
271                         break;
272
273                 case HEX:
274                         st = HEX_OK;
275                         /* no break */
276                 case HEX_OK:
277                         if (c >= '0' && c <= '9') {
278                                 if (add_to_res(c - '0', &res1, 16) < 0)
279                                         st = ERROR;
280                         }
281                         else if (c >= 'a' && c <= 'f') {
282                                 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
283                                         st = ERROR;
284                         }
285                         else if (c >= 'A' && c <= 'F') {
286                                 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
287                                         st = ERROR;
288                         }
289                         else {
290                                 st = ERROR;
291                         }
292                         break;
293
294
295                 case OCTAL_OK:
296                         if (c >= '0' && c <= '7') {
297                                 if (add_to_res(c - '0', &res1, 8) < 0)
298                                         st = ERROR;
299                         }
300                         else {
301                                 st = ERROR;
302                         }
303                         break;
304
305                 case BIN:
306                         st = BIN_OK;
307                         /* no break */
308                 case BIN_OK:
309                         if (c >= '0' && c <= '1') {
310                                 if (add_to_res(c - '0', &res1, 2) < 0)
311                                         st = ERROR;
312                         }
313                         else {
314                                 st = ERROR;
315                         }
316                         break;
317
318 #ifdef CMDLINE_HAVE_FLOAT
319                 case FLOAT_POS:
320                         if (c >= '0' && c <= '9') {
321                                 if (add_to_res(c - '0', &res2, 10) < 0)
322                                         st = ERROR;
323                                 else
324                                         st = FLOAT_POS_OK;
325                                 res3 = 10;
326                         }
327                         else {
328                                 st = ERROR;
329                         }
330                         break;
331
332                 case FLOAT_NEG:
333                         if (c >= '0' && c <= '9') {
334                                 if (add_to_res(c - '0', &res2, 10) < 0)
335                                         st = ERROR;
336                                 else
337                                         st = FLOAT_NEG_OK;
338                                 res3 = 10;
339                         }
340                         else {
341                                 st = ERROR;
342                         }
343                         break;
344
345                 case FLOAT_POS_OK:
346                         if (c >= '0' && c <= '9') {
347                                 if (add_to_res(c - '0', &res2, 10) < 0)
348                                         st = ERROR;
349                                 if (add_to_res(0, &res3, 10) < 0)
350                                         st = ERROR;
351                         }
352                         else {
353                                 st = ERROR;
354                         }
355                         break;
356
357                 case FLOAT_NEG_OK:
358                         if (c >= '0' && c <= '9') {
359                                 if (add_to_res(c - '0', &res2, 10) < 0)
360                                         st = ERROR;
361                                 if (add_to_res(0, &res3, 10) < 0)
362                                         st = ERROR;
363                         }
364                         else {
365                                 st = ERROR;
366                         }
367                         break;
368 #endif
369
370                 default:
371                         debug_printf("not impl ");
372
373                 }
374
375 #ifdef CMDLINE_HAVE_FLOAT
376                 debug_printf("(%"PRIu32")  (%"PRIu32")  (%"PRIu32")\n",
377                              res1, res2, res3);
378 #else
379                 debug_printf("(%"PRIu32")\n", res1);
380 #endif
381
382                 buf ++;
383                 c = *buf;
384
385                 /* token too long */
386                 if (buf-srcbuf > 127)
387                         return -1;
388         }
389
390         switch (st) {
391         case ZERO_OK:
392         case DEC_POS_OK:
393         case HEX_OK:
394         case OCTAL_OK:
395         case BIN_OK:
396                 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
397                         if (res)
398                                 *(int8_t *)res = (int8_t) res1;
399                         return (buf-srcbuf);
400                 }
401                 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
402                         if (res)
403                                 *(int16_t *)res = (int16_t) res1;
404                         return (buf-srcbuf);
405                 }
406                 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
407                         if (res)
408                                 *(int32_t *)res = (int32_t) res1;
409                         return (buf-srcbuf);
410                 }
411                 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
412                         if (res)
413                                 *(uint8_t *)res = (uint8_t) res1;
414                         return (buf-srcbuf);
415                 }
416                 else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
417                         if (res)
418                                 *(uint16_t *)res = (uint16_t) res1;
419                         return (buf-srcbuf);
420                 }
421                 else if ( nd.type == UINT32 ) {
422                         if (res)
423                                 *(uint32_t *)res = (uint32_t) res1;
424                         return (buf-srcbuf);
425                 }
426                 else if ( nd.type == UINT64 ) {
427                         if (res)
428                                 *(uint64_t *)res = res1;
429                         return (buf-srcbuf);
430                 }
431 #ifdef CMDLINE_HAVE_FLOAT
432                 else if ( nd.type == FLOAT ) {
433                         if (res)
434                                 *(float *)res = (float)res1;
435                         return (buf-srcbuf);
436                 }
437 #endif
438                 else {
439                         return -1;
440                 }
441                 break;
442
443         case DEC_NEG_OK:
444                 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
445                         if (res)
446                                 *(int8_t *)res = (int8_t) (-res1);
447                         return (buf-srcbuf);
448                 }
449                 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
450                         if (res)
451                                 *(int16_t *)res = (int16_t) (-res1);
452                         return (buf-srcbuf);
453                 }
454                 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
455                         if (res)
456                                 *(int32_t *)res = (int32_t) (-res1);
457                         return (buf-srcbuf);
458                 }
459 #ifdef CMDLINE_HAVE_FLOAT
460                 else if ( nd.type == FLOAT ) {
461                         if (res)
462                                 *(float *)res = - (float)res1;
463                         return (buf-srcbuf);
464                 }
465 #endif
466                 else {
467                         return -1;
468                 }
469                 break;
470
471 #ifdef CMDLINE_HAVE_FLOAT
472         case FLOAT_POS:
473         case FLOAT_POS_OK:
474                 if ( nd.type == FLOAT ) {
475                         if (res)
476                                 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
477                         return (buf-srcbuf);
478
479                 }
480                 else {
481                         return -1;
482                 }
483                 break;
484
485         case FLOAT_NEG:
486         case FLOAT_NEG_OK:
487                 if ( nd.type == FLOAT ) {
488                         if (res)
489                                 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
490                         return (buf-srcbuf);
491
492                 }
493                 else {
494                         return -1;
495                 }
496                 break;
497 #endif
498         default:
499                 debug_printf("error\n");
500                 return -1;
501         }
502 }
503
504
505 /* parse an int or a float */
506 static int
507 cmdline_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
508 {
509         struct cmdline_token_num_data nd;
510
511         memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
512
513         /* should not happen.... don't so this test */
514         /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
515         /* return -1; */
516
517         strncpy(dstbuf, num_help[nd.type], size);
518         dstbuf[size-1] = '\0';
519         return 0;
520 }
521
522
523 struct cmdline_token_ops cmdline_token_num_ops = {
524         .parse = cmdline_parse_num,
525         .complete_start = NULL,
526         .complete_iterate = NULL,
527         .complete_end = NULL,
528         .help = cmdline_help_num,
529 };