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.
33 #include "cmdline_parse.h"
34 #include "cmdline_parse_num.h"
36 //#define debug_printf(args...) printf(args)
37 #define debug_printf(args...) do {} while(0)
39 /* XXX to remove ? there should be something better */
42 #define U16_MIN 0x0000
43 #define U16_MAX 0xFFFF
44 #define U32_MIN 0x00000000
45 #define U32_MAX 0xFFFFFFFF
48 #define S16_MIN 0x8000
49 #define S16_MAX 0x7FFF
50 #define S32_MIN 0x80000000
51 #define S32_MAX 0x7FFFFFFF
54 struct cmdline_token_ops cmdline_token_num_ops = {
55 .parse = cmdline_parse_num,
56 .complete_get_nb = NULL,
57 .complete_get_elt = NULL,
58 .get_help = cmdline_get_help_num,
62 enum num_parse_state_t {
72 FIRST_OK, /* not used */
83 /* Keep it sync with enum in .h */
84 static const char * num_help[] = {
85 "UINT8", "UINT16", "UINT32",
86 "INT8", "INT16", "INT32",
87 #ifndef CMDLINE_NO_FLOAT
93 add_to_res(unsigned int c, unsigned int *res, unsigned int base)
96 if ( (U32_MAX - c) / base < *res ) {
100 *res = *res * base + c ;
105 /* parse an int or a float */
107 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
109 struct cmdline_token_num_data nd;
110 enum num_parse_state_t st = START;
111 const char * buf = srcbuf;
114 #ifndef CMDLINE_NO_FLOAT
115 uint32_t res2 = 0, res3 = 1;
118 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
120 while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
121 debug_printf("%c %x -> ", c, c);
130 #ifndef CMDLINE_NO_FLOAT
136 else if (c >= '1' && c <= '9') {
137 if (add_to_res(c - '0', &res1, 10) < 0)
154 #ifndef CMDLINE_NO_FLOAT
160 else if (c >= '0' && c <= '7') {
161 if (add_to_res(c - '0', &res1, 10) < 0)
172 if (c >= '0' && c <= '9') {
173 if (add_to_res(c - '0', &res1, 10) < 0)
178 #ifndef CMDLINE_NO_FLOAT
190 if (c >= '0' && c <= '9') {
191 if (add_to_res(c - '0', &res1, 10) < 0)
194 #ifndef CMDLINE_NO_FLOAT
205 if (c >= '0' && c <= '9') {
206 if (add_to_res(c - '0', &res1, 10) < 0)
209 #ifndef CMDLINE_NO_FLOAT
223 if (c >= '0' && c <= '9') {
224 if (add_to_res(c - '0', &res1, 16) < 0)
227 else if (c >= 'a' && c <= 'f') {
228 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
231 else if (c >= 'A' && c <= 'F') {
232 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
242 if (c >= '0' && c <= '7') {
243 if (add_to_res(c - '0', &res1, 8) < 0)
255 if (c >= '0' && c <= '1') {
256 if (add_to_res(c - '0', &res1, 2) < 0)
264 #ifndef CMDLINE_NO_FLOAT
266 if (c >= '0' && c <= '9') {
267 if (add_to_res(c - '0', &res2, 10) < 0)
279 if (c >= '0' && c <= '9') {
280 if (add_to_res(c - '0', &res2, 10) < 0)
292 if (c >= '0' && c <= '9') {
293 if (add_to_res(c - '0', &res2, 10) < 0)
295 if (add_to_res(0, &res3, 10) < 0)
304 if (c >= '0' && c <= '9') {
305 if (add_to_res(c - '0', &res2, 10) < 0)
307 if (add_to_res(0, &res3, 10) < 0)
317 debug_printf("not impl ");
321 #ifdef CMDLINE_NO_FLOAT
322 debug_printf("(%"PRIu32")\n", res1);
324 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
332 if (buf-srcbuf > 127)
342 if ( nd.type == INT8 && res1 <= S08_MAX ) {
344 *(int8_t *)res = (int8_t) res1;
347 else if ( nd.type == INT16 && res1 <= S16_MAX ) {
349 *(int16_t *)res = (int16_t) res1;
352 else if ( nd.type == INT32 && res1 <= S32_MAX ) {
354 *(int32_t *)res = (int32_t) res1;
357 else if ( nd.type == UINT8 && res1 <= U08_MAX ) {
359 *(uint8_t *)res = (uint8_t) res1;
362 else if (nd.type == UINT16 && res1 <= U16_MAX ) {
364 *(uint16_t *)res = (uint16_t) res1;
367 else if ( nd.type == UINT32 ) {
369 *(uint32_t *)res = (uint32_t) res1;
372 #ifndef CMDLINE_NO_FLOAT
373 else if ( nd.type == FLOAT ) {
375 *(float *)res = (float)res1;
385 if ( nd.type == INT8 && res1 <= S08_MAX + 1 ) {
387 *(int8_t *)res = - (int8_t) res1;
390 else if ( nd.type == INT16 && res1 <= (uint16_t)S16_MAX + 1 ) {
392 *(int16_t *)res = - (int16_t) res1;
395 else if ( nd.type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
397 *(int32_t *)res = - (int32_t) res1;
400 #ifndef CMDLINE_NO_FLOAT
401 else if ( nd.type == FLOAT ) {
403 *(float *)res = - (float)res1;
412 #ifndef CMDLINE_NO_FLOAT
415 if ( nd.type == FLOAT ) {
417 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
428 if ( nd.type == FLOAT ) {
430 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
440 debug_printf("error\n");
447 /* parse an int or a float */
449 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
451 struct cmdline_token_num_data nd;
453 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
455 /* should not happen.... don't so this test */
456 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
459 strncpy(dstbuf, num_help[nd.type], size);
460 dstbuf[size-1] = '\0';