diff options
author | Jan Kratochvil <jan.kratochvil@redhat.com> | 2015-06-15 17:04:56 +0200 |
---|---|---|
committer | Jan Kratochvil <jan.kratochvil@redhat.com> | 2015-06-15 17:04:56 +0200 |
commit | 03aef70fecec83e109c65337a59600ad36def6bf (patch) | |
tree | ed240e26dc4d64cc4536b2b371523c8b6e9e9919 /gdb/common | |
parent | d1168cc1a9825b96747440b0bd1f82c4498e6343 (diff) | |
download | gdb-03aef70fecec83e109c65337a59600ad36def6bf.zip gdb-03aef70fecec83e109c65337a59600ad36def6bf.tar.gz gdb-03aef70fecec83e109c65337a59600ad36def6bf.tar.bz2 |
Move utility functions to common/
gdb/ChangeLog
2015-06-15 Aleksandar Ristovski <aristovski@qnx.com
Jan Kratochvil <jan.kratochvil@redhat.com>
Move utility functions to common/.
* cli/cli-utils.c (skip_spaces, skip_spaces_const, skip_to_space_const):
Move defs to common/common-utils.c.
* cli/cli-utils.h (skip_spaces, skip_spaces_const, skip_to_space)
(skip_to_space_const): Move decls to common/common-utils.h.
* common/common-defs.h: Move include of common-types.h before
common-utils.h.
* common/common-utils.c: Include host-defs.h and ctype.h.
(HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move
from utils.c.
(skip_spaces, skip_spaces_const, skip_to_space_const): Move from
cli/cli-utils.c.
* common/common-utils.h (strtoulst): Move decl from utils.h.
(skip_spaces, skip_spaces_const, skip_to_space, skip_to_space_const):
Move from cli/cli-utils.h.
* common/host-defs.h: Include limits.h.
(TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved from defs.h.
(skip_spaces, skip_spaces_const): Move decls from cli/cli-utils.h.
* defs.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Move to
common/common-utils.h.
* utils.c (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int)
(strtoulst): Move to common/common-utils.c.
* utils.h (strtoulst): Moved decl to common/common-utils.h.
Diffstat (limited to 'gdb/common')
-rw-r--r-- | gdb/common/common-defs.h | 2 | ||||
-rw-r--r-- | gdb/common/common-utils.c | 137 | ||||
-rw-r--r-- | gdb/common/common-utils.h | 20 | ||||
-rw-r--r-- | gdb/common/host-defs.h | 21 |
4 files changed, 179 insertions, 1 deletions
diff --git a/gdb/common/common-defs.h b/gdb/common/common-defs.h index 62d9de5..2be0d7d 100644 --- a/gdb/common/common-defs.h +++ b/gdb/common/common-defs.h @@ -41,10 +41,10 @@ #include "gdb/signals.h" #include "gdb_locale.h" #include "ptid.h" +#include "common-types.h" #include "common-utils.h" #include "gdb_assert.h" #include "errors.h" -#include "common-types.h" #include "print-utils.h" #include "common-debug.h" #include "cleanups.h" diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index 2925dd5..e86ed5b 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -18,6 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "common-defs.h" +#include "host-defs.h" +#include <ctype.h> /* The xmalloc() (libiberty.h) family of memory management routines. @@ -151,3 +153,138 @@ savestring (const char *ptr, size_t len) p[len] = 0; return p; } + +/* The bit offset of the highest byte in a ULONGEST, for overflow + checking. */ + +#define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT) + +/* True (non-zero) iff DIGIT is a valid digit in radix BASE, + where 2 <= BASE <= 36. */ + +static int +is_digit_in_base (unsigned char digit, int base) +{ + if (!isalnum (digit)) + return 0; + if (base <= 10) + return (isdigit (digit) && digit < base + '0'); + else + return (isdigit (digit) || tolower (digit) < base - 10 + 'a'); +} + +static int +digit_to_int (unsigned char c) +{ + if (isdigit (c)) + return c - '0'; + else + return tolower (c) - 'a' + 10; +} + +/* As for strtoul, but for ULONGEST results. */ + +ULONGEST +strtoulst (const char *num, const char **trailer, int base) +{ + unsigned int high_part; + ULONGEST result; + int minus = 0; + int i = 0; + + /* Skip leading whitespace. */ + while (isspace (num[i])) + i++; + + /* Handle prefixes. */ + if (num[i] == '+') + i++; + else if (num[i] == '-') + { + minus = 1; + i++; + } + + if (base == 0 || base == 16) + { + if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X')) + { + i += 2; + if (base == 0) + base = 16; + } + } + + if (base == 0 && num[i] == '0') + base = 8; + + if (base == 0) + base = 10; + + if (base < 2 || base > 36) + { + errno = EINVAL; + return 0; + } + + result = high_part = 0; + for (; is_digit_in_base (num[i], base); i += 1) + { + result = result * base + digit_to_int (num[i]); + high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN); + result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1; + if (high_part > 0xff) + { + errno = ERANGE; + result = ~ (ULONGEST) 0; + high_part = 0; + minus = 0; + break; + } + } + + if (trailer != NULL) + *trailer = &num[i]; + + result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN); + if (minus) + return -result; + else + return result; +} + +/* See documentation in cli-utils.h. */ + +char * +skip_spaces (char *chp) +{ + if (chp == NULL) + return NULL; + while (*chp && isspace (*chp)) + chp++; + return chp; +} + +/* A const-correct version of the above. */ + +const char * +skip_spaces_const (const char *chp) +{ + if (chp == NULL) + return NULL; + while (*chp && isspace (*chp)) + chp++; + return chp; +} + +/* See documentation in cli-utils.h. */ + +const char * +skip_to_space_const (const char *chp) +{ + if (chp == NULL) + return NULL; + while (*chp && !isspace (*chp)) + chp++; + return chp; +} diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index cd2665a..eef5e6b 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -77,4 +77,24 @@ startswith (const char *string, const char *pattern) return strncmp (string, pattern, strlen (pattern)) == 0; } +ULONGEST strtoulst (const char *num, const char **trailer, int base); + +/* Skip leading whitespace characters in INP, returning an updated + pointer. If INP is NULL, return NULL. */ + +extern char *skip_spaces (char *inp); + +/* A const-correct version of the above. */ + +extern const char *skip_spaces_const (const char *inp); + +/* Skip leading non-whitespace characters in INP, returning an updated + pointer. If INP is NULL, return NULL. */ + +#define skip_to_space(INP) ((char *) skip_to_space_const (INP)) + +/* A const-correct version of the above. */ + +extern const char *skip_to_space_const (const char *inp); + #endif diff --git a/gdb/common/host-defs.h b/gdb/common/host-defs.h index 6c67034..7bc41a2 100644 --- a/gdb/common/host-defs.h +++ b/gdb/common/host-defs.h @@ -19,6 +19,27 @@ #ifndef HOST_DEFS_H #define HOST_DEFS_H +#include <limits.h> + +/* Static host-system-dependent parameters for GDB. */ + +/* * Number of bits in a char or unsigned char for the target machine. + Just like CHAR_BIT in <limits.h> but describes the target machine. */ +#if !defined (TARGET_CHAR_BIT) +#define TARGET_CHAR_BIT 8 +#endif + +/* * If we picked up a copy of CHAR_BIT from a configuration file + (which may get it by including <limits.h>) then use it to set + the number of bits in a host char. If not, use the same size + as the target. */ + +#if defined (CHAR_BIT) +#define HOST_CHAR_BIT CHAR_BIT +#else +#define HOST_CHAR_BIT TARGET_CHAR_BIT +#endif + #ifdef __MSDOS__ # define CANT_FORK # define GLOBAL_CURDIR |