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 NO_PARSE_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;
113 uint32_t res1=0, res2=0, res3=1;
115 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
117 while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
118 debug_printf("%c %x -> ", c, c);
127 #ifndef NO_PARSE_FLOAT
133 else if (c >= '1' && c <= '9') {
134 if (add_to_res(c - '0', &res1, 10) < 0)
151 #ifndef NO_PARSE_FLOAT
157 else if (c >= '0' && c <= '7') {
158 if (add_to_res(c - '0', &res1, 10) < 0)
169 if (c >= '0' && c <= '9') {
170 if (add_to_res(c - '0', &res1, 10) < 0)
175 #ifndef NO_PARSE_FLOAT
187 if (c >= '0' && c <= '9') {
188 if (add_to_res(c - '0', &res1, 10) < 0)
191 #ifndef NO_PARSE_FLOAT
202 if (c >= '0' && c <= '9') {
203 if (add_to_res(c - '0', &res1, 10) < 0)
206 #ifndef NO_PARSE_FLOAT
220 if (c >= '0' && c <= '9') {
221 if (add_to_res(c - '0', &res1, 16) < 0)
224 else if (c >= 'a' && c <= 'f') {
225 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
228 else if (c >= 'A' && c <= 'F') {
229 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
239 if (c >= '0' && c <= '7') {
240 if (add_to_res(c - '0', &res1, 8) < 0)
252 if (c >= '0' && c <= '1') {
253 if (add_to_res(c - '0', &res1, 2) < 0)
261 #ifndef NO_PARSE_FLOAT
263 if (c >= '0' && c <= '9') {
264 if (add_to_res(c - '0', &res2, 10) < 0)
276 if (c >= '0' && c <= '9') {
277 if (add_to_res(c - '0', &res2, 10) < 0)
289 if (c >= '0' && c <= '9') {
290 if (add_to_res(c - '0', &res2, 10) < 0)
292 if (add_to_res(0, &res3, 10) < 0)
301 if (c >= '0' && c <= '9') {
302 if (add_to_res(c - '0', &res2, 10) < 0)
304 if (add_to_res(0, &res3, 10) < 0)
314 debug_printf("not impl ");
318 /* XXX uint32_t et %d */
319 debug_printf("(%d) (%d) (%d)\n", res1, res2, res3);
325 if (buf-srcbuf > 127)
335 if ( nd.type == INT8 && res1 <= S08_MAX ) {
337 *(int8_t *)res = (int8_t) res1;
340 else if ( nd.type == INT16 && res1 <= S16_MAX ) {
342 *(int16_t *)res = (int16_t) res1;
345 else if ( nd.type == INT32 && res1 <= S32_MAX ) {
347 *(int32_t *)res = (int32_t) res1;
350 else if ( nd.type == UINT8 && res1 <= U08_MAX ) {
352 *(uint8_t *)res = (uint8_t) res1;
355 else if (nd.type == UINT16 && res1 <= U16_MAX ) {
357 *(uint16_t *)res = (uint16_t) res1;
360 else if ( nd.type == UINT32 ) {
362 *(uint32_t *)res = (uint32_t) res1;
365 #ifndef NO_PARSE_FLOAT
366 else if ( nd.type == FLOAT ) {
368 *(float *)res = (float)res1;
378 if ( nd.type == INT8 && res1 <= S08_MAX + 1 ) {
380 *(int8_t *)res = - (int8_t) res1;
383 else if ( nd.type == INT16 && res1 <= (uint16_t)S16_MAX + 1 ) {
385 *(int16_t *)res = - (int16_t) res1;
388 else if ( nd.type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
390 *(int32_t *)res = - (int32_t) res1;
393 #ifndef NO_PARSE_FLOAT
394 else if ( nd.type == FLOAT ) {
396 *(float *)res = - (float)res1;
405 #ifndef NO_PARSE_FLOAT
408 if ( nd.type == FLOAT ) {
410 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
421 if ( nd.type == FLOAT ) {
423 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
433 debug_printf("error\n");
440 /* parse an int or a float */
442 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
444 struct cmdline_token_num_data nd;
446 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
448 /* should not happen.... don't so this test */
449 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
452 strncpy(dstbuf, num_help[nd.type], size);
453 dstbuf[size-1] = '\0';