initial revision
[ucgine.git] / lib / cmd / ucg_cmd_parse_num.c
1 /*
2  * Copyright (c) 2009-2015, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /*-
29  * Copyright (c) <2010>, Intel Corporation
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  *
36  * - Redistributions of source code must retain the above copyright
37  *   notice, this list of conditions and the following disclaimer.
38  *
39  * - Redistributions in binary form must reproduce the above copyright
40  *   notice, this list of conditions and the following disclaimer in
41  *   the documentation and/or other materials provided with the
42  *   distribution.
43  *
44  * - Neither the name of Intel Corporation nor the names of its
45  *   contributors may be used to endorse or promote products derived
46  *   from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59  * 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 "ucg_cmd_parse.h"
69 #include "ucg_cmd_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 UCG_CMD_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 ucg_cmd_tk_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 UCG_CMD_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 cmd_parse_num(ucg_cmd_tk_hdr_t *tk, const char *srcbuf,
155         void *res, unsigned ressize)
156 {
157         struct ucg_cmd_tk_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 UCG_CMD_HAVE_FLOAT
163         uint64_t res2 = 0, res3 = 1;
164 #endif
165
166         memcpy(&nd, &((struct ucg_cmd_tk_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 UCG_CMD_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 UCG_CMD_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 UCG_CMD_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 UCG_CMD_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 UCG_CMD_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 UCG_CMD_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 UCG_CMD_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 0;
400                 }
401                 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
402                         if (res)
403                                 *(int16_t *)res = (int16_t) res1;
404                         return 0;
405                 }
406                 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
407                         if (res)
408                                 *(int32_t *)res = (int32_t) res1;
409                         return 0;
410                 }
411                 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
412                         if (res)
413                                 *(uint8_t *)res = (uint8_t) res1;
414                         return 0;
415                 }
416                 else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
417                         if (res)
418                                 *(uint16_t *)res = (uint16_t) res1;
419                         return 0;
420                 }
421                 else if ( nd.type == UINT32 ) {
422                         if (res)
423                                 *(uint32_t *)res = (uint32_t) res1;
424                         return 0;
425                 }
426                 else if ( nd.type == UINT64 ) {
427                         if (res)
428                                 *(uint64_t *)res = res1;
429                         return 0;
430                 }
431 #ifdef UCG_CMD_HAVE_FLOAT
432                 else if ( nd.type == FLOAT ) {
433                         if (res)
434                                 *(float *)res = (float)res1;
435                         return 0;
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 0;
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 0;
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 0;
458                 }
459 #ifdef UCG_CMD_HAVE_FLOAT
460                 else if ( nd.type == FLOAT ) {
461                         if (res)
462                                 *(float *)res = - (float)res1;
463                         return 0;
464                 }
465 #endif
466                 else {
467                         return -1;
468                 }
469                 break;
470
471 #ifdef UCG_CMD_HAVE_FLOAT
472         case FLOAT_POS:
473         case FLOAT_POS_OK:
474                 if ( nd.type == FLOAT ) {
475                         if (res)
476                                 *(float *)res = (float)res1 +
477                                         ((float)res2 / (float)res3);
478                         return 0;
479
480                 }
481                 else {
482                         return -1;
483                 }
484                 break;
485
486         case FLOAT_NEG:
487         case FLOAT_NEG_OK:
488                 if ( nd.type == FLOAT ) {
489                         if (res)
490                                 *(float *)res = - ((float)res1 +
491 ((float)res2 / (float)res3));
492                         return 0;
493
494                 }
495                 else {
496                         return -1;
497                 }
498                 break;
499 #endif
500         default:
501                 debug_printf("error\n");
502                 return -1;
503         }
504 }
505
506
507 /* parse an int or a float */
508 static int
509 cmd_help_num(ucg_cmd_tk_hdr_t *tk, char *dstbuf,
510         unsigned int size)
511 {
512         struct ucg_cmd_tk_num_data nd;
513
514         memcpy(&nd, &((struct ucg_cmd_tk_num *)tk)->num_data, sizeof(nd));
515
516         /* should not happen.... don't so this test */
517         /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
518         /* return -1; */
519
520         strncpy(dstbuf, num_help[nd.type], size);
521         dstbuf[size-1] = '\0';
522         return 0;
523 }
524
525
526 struct ucg_cmd_tk_ops ucg_cmd_tk_num_ops = {
527         .parse = cmd_parse_num,
528         .complete_start = NULL,
529         .complete_iterate = NULL,
530         .complete_end = NULL,
531         .help = cmd_help_num,
532 };