From 597de3180f30893ccb2399fbbad329b56a1b9958 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Wed, 6 Jan 2010 12:01:49 +1300 Subject: [PATCH] Use decimal values unless indicated by 0x. --- main.c | 2 +- stab.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index c10c018..ff5343c 100644 --- a/main.c +++ b/main.c @@ -483,7 +483,7 @@ static int cmd_eval(char **arg) return -1; } - printf("%04x", addr); + printf("0x%04x", addr); offset = addr; if (!stab_find(&offset, &name)) { printf(" = %s", name); diff --git a/stab.c b/stab.c index 665d719..63b7f06 100644 --- a/stab.c +++ b/stab.c @@ -183,17 +183,33 @@ static int token_len; static int token_mult; static int token_sum; -static void token_add(void) +static int token_add(void) { int low = 0; int high = by_name.len - 1; + int i; if (!token_len) - return; + return 0; token_buf[token_len] = 0; token_len = 0; + /* Is it a decimal? */ + i = 0; + while (token_buf[i] && isdigit(token_buf[i])) + i++; + if (!token_buf[i]) { + token_sum += token_mult * atoi(token_buf); + return 0; + } + + /* Is it hex? */ + if (token_buf[0] == '0' && tolower(token_buf[1]) == 'x') { + token_sum += token_mult * strtol(token_buf + 2, NULL, 16); + return 0; + } + /* Look up the name in the symbol table */ while (low <= high) { int mid = (low + high) / 2; @@ -202,7 +218,7 @@ static void token_add(void) if (!cmp) { token_sum += token_mult * (int)sym->addr; - return; + return 0; } if (cmp < 0) @@ -211,8 +227,8 @@ static void token_add(void) high = mid - 1; } - /* Not found? Try to parse it the old way */ - token_sum += token_mult * strtol(token_buf, NULL, 16); + fprintf(stderr, "stab: unknown token: %s\n", token_buf); + return -1; } int stab_parse(const char *text, int *addr) @@ -228,7 +244,8 @@ int stab_parse(const char *text, int *addr) if (token_len + 1 < sizeof(token_buf)) token_buf[token_len++] = *text; } else { - token_add(); + if (token_add() < 0) + return -1; if (*text == '+') token_mult = 1; if (*text == '-') @@ -238,7 +255,9 @@ int stab_parse(const char *text, int *addr) text++; } - token_add(); + if (token_add() < 0) + return -1; + *addr = token_sum; return 0; }