2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
34 #include "cmdline_parse.h"
35 #include "cmdline_parse_num.h"
37 //#define debug_printf(args...) printf(args)
38 #define debug_printf(args...) do {} while(0)
40 struct cmdline_token_ops cmdline_token_num_ops = {
41 .parse = cmdline_parse_num,
42 .complete_get_nb = NULL,
43 .complete_get_elt = NULL,
44 .get_help = cmdline_get_help_num,
48 enum num_parse_state_t {
58 FIRST_OK, /* not used */
69 /* Keep it sync with enum in .h */
70 static const char * num_help[] = {
71 "UINT8", "UINT16", "UINT32",
72 "INT8", "INT16", "INT32",
73 #ifndef CMDLINE_NO_FLOAT
79 add_to_res(unsigned int c, unsigned int *res, unsigned int base)
82 if ( (UINT32_MAX - c) / base < *res ) {
86 *res = *res * base + c ;
91 /* parse an int or a float */
93 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
95 struct cmdline_token_num_data nd;
96 enum num_parse_state_t st = START;
97 const char * buf = srcbuf;
100 #ifndef CMDLINE_NO_FLOAT
101 uint32_t res2 = 0, res3 = 1;
104 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
106 while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
107 debug_printf("%c %x -> ", c, c);
116 #ifndef CMDLINE_NO_FLOAT
122 else if (c >= '1' && c <= '9') {
123 if (add_to_res(c - '0', &res1, 10) < 0)
140 #ifndef CMDLINE_NO_FLOAT
146 else if (c >= '0' && c <= '7') {
147 if (add_to_res(c - '0', &res1, 10) < 0)
158 if (c >= '0' && c <= '9') {
159 if (add_to_res(c - '0', &res1, 10) < 0)
164 #ifndef CMDLINE_NO_FLOAT
176 if (c >= '0' && c <= '9') {
177 if (add_to_res(c - '0', &res1, 10) < 0)
180 #ifndef CMDLINE_NO_FLOAT
191 if (c >= '0' && c <= '9') {
192 if (add_to_res(c - '0', &res1, 10) < 0)
195 #ifndef CMDLINE_NO_FLOAT
209 if (c >= '0' && c <= '9') {
210 if (add_to_res(c - '0', &res1, 16) < 0)
213 else if (c >= 'a' && c <= 'f') {
214 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
217 else if (c >= 'A' && c <= 'F') {
218 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
228 if (c >= '0' && c <= '7') {
229 if (add_to_res(c - '0', &res1, 8) < 0)
241 if (c >= '0' && c <= '1') {
242 if (add_to_res(c - '0', &res1, 2) < 0)
250 #ifndef CMDLINE_NO_FLOAT
252 if (c >= '0' && c <= '9') {
253 if (add_to_res(c - '0', &res2, 10) < 0)
265 if (c >= '0' && c <= '9') {
266 if (add_to_res(c - '0', &res2, 10) < 0)
278 if (c >= '0' && c <= '9') {
279 if (add_to_res(c - '0', &res2, 10) < 0)
281 if (add_to_res(0, &res3, 10) < 0)
290 if (c >= '0' && c <= '9') {
291 if (add_to_res(c - '0', &res2, 10) < 0)
293 if (add_to_res(0, &res3, 10) < 0)
303 debug_printf("not impl ");
307 #ifdef CMDLINE_NO_FLOAT
308 debug_printf("(%"PRIu32")\n", res1);
310 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
318 if (buf-srcbuf > 127)
328 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
330 *(int8_t *)res = (int8_t) res1;
333 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
335 *(int16_t *)res = (int16_t) res1;
338 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
340 *(int32_t *)res = (int32_t) res1;
343 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
345 *(uint8_t *)res = (uint8_t) res1;
348 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
350 *(uint16_t *)res = (uint16_t) res1;
353 else if ( nd.type == UINT32 ) {
355 *(uint32_t *)res = (uint32_t) res1;
358 #ifndef CMDLINE_NO_FLOAT
359 else if ( nd.type == FLOAT ) {
361 *(float *)res = (float)res1;
371 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
373 *(int8_t *)res = - (int8_t) res1;
376 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
378 *(int16_t *)res = - (int16_t) res1;
381 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
383 *(int32_t *)res = - (int32_t) res1;
386 #ifndef CMDLINE_NO_FLOAT
387 else if ( nd.type == FLOAT ) {
389 *(float *)res = - (float)res1;
398 #ifndef CMDLINE_NO_FLOAT
401 if ( nd.type == FLOAT ) {
403 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
414 if ( nd.type == FLOAT ) {
416 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
426 debug_printf("error\n");
433 /* parse an int or a float */
435 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
437 struct cmdline_token_num_data nd;
439 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
441 /* should not happen.... don't so this test */
442 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
445 strncpy(dstbuf, num_help[nd.type], size);
446 dstbuf[size-1] = '\0';