cmdline (merge-intel): use maximum int values defines from stdint.h
[libcmdline.git] / src / lib / cmdline_parse_num.c
index 71075b4..6d7adb5 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include <stdio.h>
+#include <stdint.h>
 #include <inttypes.h>
 #include <ctype.h>
 #include <string.h>
 //#define debug_printf(args...) printf(args)
 #define debug_printf(args...) do {} while(0)
 
-/* XXX to remove ? there should be something better */
-#define U08_MIN 0x00
-#define U08_MAX 0xFF
-#define U16_MIN 0x0000
-#define U16_MAX 0xFFFF
-#define U32_MIN 0x00000000
-#define U32_MAX 0xFFFFFFFF
-#define S08_MIN 0x80
-#define S08_MAX 0x7F
-#define S16_MIN 0x8000
-#define S16_MAX 0x7FFF
-#define S32_MIN 0x80000000
-#define S32_MAX 0x7FFFFFFF
-
-
 struct cmdline_token_ops cmdline_token_num_ops = {
        .parse = cmdline_parse_num,
        .complete_get_nb = NULL,
@@ -84,7 +70,7 @@ enum num_parse_state_t {
 static const char * num_help[] = {
        "UINT8", "UINT16", "UINT32",
        "INT8", "INT16", "INT32",
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
        "FLOAT",
 #endif
 };
@@ -93,7 +79,7 @@ static inline int
 add_to_res(unsigned int c, unsigned int *res, unsigned int base)
 {
        /* overflow */
-       if ( (U32_MAX - c) / base < *res ) {
+       if ( (UINT32_MAX - c) / base < *res ) {
                return -1;
        }
 
@@ -110,7 +96,10 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
        enum num_parse_state_t st = START;
        const char * buf = srcbuf;
        char c = *buf;
-       uint32_t res1=0, res2=0, res3=1;
+       uint32_t res1 = 0;
+#ifndef CMDLINE_NO_FLOAT
+       uint32_t res2 = 0, res3 = 1;
+#endif
 
        memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
 
@@ -124,7 +113,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                        else if (c == '0') {
                                st = ZERO_OK;
                        }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                        else if (c == '.') {
                                st = FLOAT_POS;
                                res1 = 0;
@@ -148,7 +137,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                        else if (c == 'b') {
                                st = BIN;
                        }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                        else if (c == '.') {
                                st = FLOAT_POS;
                                res1 = 0;
@@ -172,7 +161,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                else
                                        st = DEC_NEG_OK;
                        }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                        else if (c == '.') {
                                res1 = 0;
                                st = FLOAT_NEG;
@@ -188,7 +177,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
                        }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                        else if (c == '.') {
                                st = FLOAT_NEG;
                        }
@@ -203,7 +192,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
                        }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                        else if (c == '.') {
                                st = FLOAT_POS;
                        }
@@ -258,7 +247,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                        }
                        break;
 
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                case FLOAT_POS:
                        if (c >= '0' && c <= '9') {
                                if (add_to_res(c - '0', &res2, 10) < 0)
@@ -315,8 +304,12 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
 
                }
 
-               /* XXX uint32_t et %d */
-               debug_printf("(%d)  (%d)  (%d)\n", res1, res2, res3);
+#ifdef CMDLINE_NO_FLOAT
+               debug_printf("(%"PRIu32")\n", res1);
+#else
+               debug_printf("(%"PRIu32")  (%"PRIu32")  (%"PRIu32")\n",
+                            res1, res2, res3);
+#endif
 
                buf ++;
                c = *buf;
@@ -332,27 +325,27 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
        case HEX_OK:
        case OCTAL_OK:
        case BIN_OK:
-               if ( nd.type == INT8 && res1 <= S08_MAX ) {
+               if ( nd.type == INT8 && res1 <= INT8_MAX ) {
                        if (res)
                                *(int8_t *)res = (int8_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == INT16 && res1 <= S16_MAX ) {
+               else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
                        if (res)
                                *(int16_t *)res = (int16_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == INT32 && res1 <= S32_MAX ) {
+               else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
                        if (res)
                                *(int32_t *)res = (int32_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == UINT8 && res1 <= U08_MAX ) {
+               else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
                        if (res)
                                *(uint8_t *)res = (uint8_t) res1;
                        return (buf-srcbuf);
                }
-               else if (nd.type == UINT16  && res1 <= U16_MAX ) {
+               else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
                        if (res)
                                *(uint16_t *)res = (uint16_t) res1;
                        return (buf-srcbuf);
@@ -362,7 +355,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                *(uint32_t *)res = (uint32_t) res1;
                        return (buf-srcbuf);
                }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                else if ( nd.type == FLOAT ) {
                        if (res)
                                *(float *)res = (float)res1;
@@ -375,22 +368,22 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                break;
 
        case DEC_NEG_OK:
-               if ( nd.type == INT8 && res1 <= S08_MAX + 1 ) {
+               if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
                        if (res)
                                *(int8_t *)res = - (int8_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == INT16 && res1 <= (uint16_t)S16_MAX + 1 ) {
+               else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
                        if (res)
                                *(int16_t *)res = - (int16_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
+               else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
                        if (res)
                                *(int32_t *)res = - (int32_t) res1;
                        return (buf-srcbuf);
                }
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
                else if ( nd.type == FLOAT ) {
                        if (res)
                                *(float *)res = - (float)res1;
@@ -402,7 +395,7 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                }
                break;
 
-#ifndef NO_PARSE_FLOAT
+#ifndef CMDLINE_NO_FLOAT
        case FLOAT_POS:
        case FLOAT_POS_OK:
                if ( nd.type == FLOAT ) {
@@ -446,8 +439,8 @@ cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int s
        memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
 
        /* should not happen.... don't so this test */
-/*     if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
-/*             return -1; */
+       /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
+       /* return -1; */
 
        strncpy(dstbuf, num_help[nd.type], size);
        dstbuf[size-1] = '\0';