9 //#define debug_printf(args...) printf(args)
10 #define debug_printf(args...) do {} while(0)
12 /* XXX to remove ?? */
15 #define U16_MIN 0x0000
16 #define U16_MAX 0xFFFF
17 #define U32_MIN 0x00000000
18 #define U32_MAX 0xFFFFFFFF
19 #define U64_MIN 0x0000000000000000
20 #define U64_MAX 0xFFFFFFFFFFFFFFFF
23 #define S16_MIN 0x8000
24 #define S16_MAX 0x7FFF
25 #define S32_MIN 0x80000000
26 #define S32_MAX 0x7FFFFFFF
27 #define S64_MIN 0x8000000000000000
28 #define S64_MAX 0x7FFFFFFFFFFFFFFF
31 struct token_ops token_num_ops = {
33 .complete_get_nb = NULL,
34 .complete_get_elt = NULL,
35 .get_help = get_help_num,
39 enum num_parse_state_t {
48 FIRST_OK, /* not used */
59 /* Keep it sync with enum in .h */
60 static const prog_char help1[] = "UINT8";
61 static const prog_char help2[] = "UINT16";
62 static const prog_char help3[] = "UINT32";
63 static const prog_char help4[] = "UINT64";
64 static const prog_char help5[] = "INT8";
65 static const prog_char help6[] = "INT16";
66 static const prog_char help7[] = "INT32";
67 static const prog_char help8[] = "INT64";
68 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
69 static const prog_char help9[] = "FLOAT";
71 static const prog_char * num_help[] = {
72 help1, help2, help3, help4,
73 help5, help6, help7, help8,
74 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
80 add_to_res(uint8_t c, uint64_t * res, uint8_t base)
83 if ( (U64_MAX - c) / base < *res ) {
87 *res = *res * base + c ;
92 /* parse an int or a float */
94 parse_num(parse_pgm_token_hdr_t * tk, const char * srcbuf, void * res)
96 struct token_num_data nd;
97 enum num_parse_state_t st = START;
98 const char * buf = srcbuf;
100 uint64_t res1=0, res2=0, res3=1;
102 memcpy_P(&nd, &((struct token_num *)tk)->num_data, sizeof(nd));
104 while ( st != ERROR && c && ! isendoftoken(c) ) {
105 debug_printf("%c %x -> ", c, c);
114 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
120 else if (c >= '1' && c <= '9') {
121 if (add_to_res(c - '0', &res1, 10) < 0)
138 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
144 else if (c >= '0' && c <= '7') {
145 if (add_to_res(c - '0', &res1, 10) < 0)
156 if (c >= '0' && c <= '9') {
157 if (add_to_res(c - '0', &res1, 10) < 0)
162 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
174 if (c >= '0' && c <= '9') {
175 if (add_to_res(c - '0', &res1, 10) < 0)
178 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
189 if (c >= '0' && c <= '9') {
190 if (add_to_res(c - '0', &res1, 10) < 0)
193 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
207 if (c >= '0' && c <= '9') {
208 if (add_to_res(c - '0', &res1, 16) < 0)
211 else if (c >= 'a' && c <= 'f') {
212 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
215 else if (c >= 'A' && c <= 'F') {
216 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
226 if (c >= '0' && c <= '7') {
227 if (add_to_res(c - '0', &res1, 8) < 0)
239 if (c >= '0' && c <= '1') {
240 if (add_to_res(c - '0', &res1, 2) < 0)
248 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
250 if (c >= '0' && c <= '9') {
251 if (add_to_res(c - '0', &res2, 10) < 0)
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)
279 if (add_to_res(0, &res3, 10) < 0)
288 if (c >= '0' && c <= '9') {
289 if (add_to_res(c - '0', &res2, 10) < 0)
291 if (add_to_res(0, &res3, 10) < 0)
301 debug_printf("not impl ");
305 debug_printf("(%d) (%d) (%d)\n",
306 (int)res1, (int)res2, (int)res3);
312 if (buf-srcbuf > 127)
322 if ( nd.type == INT8 && res1 <= S08_MAX ) {
324 *(int8_t *)res = (int8_t) res1;
327 else if ( nd.type == INT16 && res1 <= S16_MAX ) {
329 *(int16_t *)res = (int16_t) res1;
332 else if ( nd.type == INT32 && res1 <= S32_MAX ) {
334 *(int32_t *)res = (int32_t) res1;
337 else if ( nd.type == INT64 && res1 <= S64_MAX ) {
339 *(int64_t *)res = (int64_t) res1;
342 else if ( nd.type == UINT8 && res1 <= U08_MAX ) {
344 *(uint8_t *)res = (uint8_t) res1;
347 else if (nd.type == UINT16 && res1 <= U16_MAX ) {
349 *(uint16_t *)res = (uint16_t) res1;
352 else if ( nd.type == UINT32 ) {
354 *(uint32_t *)res = (uint32_t) res1;
357 else if ( nd.type == UINT64 ) {
359 *(uint64_t *)res = (uint64_t) res1;
362 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
363 else if ( nd.type == FLOAT ) {
365 *(float *)res = (float)res1;
375 if ( nd.type == INT8 && res1 <= S08_MAX + 1 ) {
377 *(int8_t *)res = - (int8_t) res1;
380 else if ( nd.type == INT16 && res1 <= (uint16_t)S16_MAX + 1 ) {
382 *(int16_t *)res = - (int16_t) res1;
385 else if ( nd.type == INT32 && res1 <= (uint32_t)S32_MAX + 1 ) {
387 *(int32_t *)res = - (int32_t) res1;
390 else if ( nd.type == INT64 && res1 <= (uint64_t)S64_MAX + 1 ) {
392 *(int64_t *)res = - (int64_t) res1;
395 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
396 else if ( nd.type == FLOAT ) {
398 *(float *)res = - (float)res1;
407 #ifndef CONFIG_MODULE_PARSE_NO_FLOAT
410 if ( nd.type == FLOAT ) {
412 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
423 if ( nd.type == FLOAT ) {
425 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
435 debug_printf("error\n");
442 /* parse an int or a float */
444 get_help_num(parse_pgm_token_hdr_t * tk, char * dstbuf, uint8_t size)
446 struct token_num_data nd;
448 memcpy_P(&nd, &((struct token_num *)tk)->num_data, sizeof(nd));
450 /* should not happen.... don't so this test */
451 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
454 strncpy_P(dstbuf, num_help[nd.type], size);
455 dstbuf[size-1] = '\0';