From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31218 invoked by alias); 6 Apr 2013 19:11:25 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 31103 invoked by uid 89); 6 Apr 2013 19:11:24 -0000 X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,DATE_IN_PAST_24_48,FSL_HELO_BARE_IP_2,KHOP_THREADED,RCVD_IN_DNSWL_NONE,RCVD_NUMERIC_HELO,RP_MATCHES_RCVD,SPF_HELO_PASS,TW_XS autolearn=ham version=3.3.1 Received: from plane.gmane.org (HELO plane.gmane.org) (80.91.229.3) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Sat, 06 Apr 2013 19:11:21 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UOYE4-0001uA-CL for gdb-patches@sourceware.org; Sat, 06 Apr 2013 20:52:32 +0200 Received: from 209.226.137.106 ([209.226.137.106]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Apr 2013 20:52:32 +0200 Received: from ARistovski by 209.226.137.106 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 06 Apr 2013 20:52:32 +0200 To: gdb-patches@sourceware.org From: Aleksandar Ristovski Subject: Re: [patch 1/6] Move utility functions to common/ Date: Sun, 07 Apr 2013 18:54:00 -0000 Message-ID: <515ECAAF.8030209@qnx.com> References: <51278984.3070208@qnx.com> <20130310210746.GB21130@host2.jankratochvil.net> <514C56BA.5050504@qnx.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------060600060005020806000309" Cc: gdb-patches@sourceware.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 In-Reply-To: <514C56BA.5050504@qnx.com> X-Virus-Found: No X-SW-Source: 2013-04/txt/msg00164.txt.bz2 This is a multi-part message in MIME format. --------------060600060005020806000309 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1443 Rebased to master e96bd93d436e464a532a7e1161e1d201c9fc50c7 On 13-03-22 09:03 AM, Aleksandar Ristovski wrote: > As per the subject... > > > ChangeLog: > > * cli/cli-utils.c (skip_spaces, skip_spaces_const): Move defs to > common/common-utils.c. > * cli/cli-utils.h (skip_spaces, skip_spaces_const): Move decls to > common/common-utils.h. > * common/common-utils.c (ctype.h): Include. > (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move > from utils.c. > (fromhex): Copy from remote.c. > (hex2bin): Move from remote.c. > (tohex): Copy from remote.c. > (bin2hex): Move from remote.c. > (skip_spaces, skip_spaces_const): Move from cli/cli-utils.c. > * common/common-utils.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved > from defs.h. > (strtoulst): Move decl from utils.h. > (hex2bin, bin2hex): Move decls from remote.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 > * remote.c (hex2bin, bin2hex): Moved defs to common/common-utils.c. > * remote.h (hex2bin, bin2hex): Moved decls 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. > > > > Thanks, > > Aleksandar > > --------------060600060005020806000309 Content-Type: text/x-patch; name="0001-Move-utility-functions-to-common.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Move-utility-functions-to-common.patch" Content-length: 14058 >From d2cb04f1e90e14d72a63fa23ec5bb6421cc9e682 Mon Sep 17 00:00:00 2001 From: Aleksandar Ristovski Date: Wed, 27 Mar 2013 09:39:05 -0400 Subject: [PATCH 1/8] Move utility functions to common/ * cli/cli-utils.c (skip_spaces, skip_spaces_const): Move defs to common/common-utils.c. * cli/cli-utils.h (skip_spaces, skip_spaces_const): Move decls to common/common-utils.h. * common/common-utils.c (ctype.h): Include. (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move from utils.c. (fromhex): Copy from remote.c. (hex2bin): Move from remote.c. (tohex): Copy from remote.c. (bin2hex): Move from remote.c. (skip_spaces, skip_spaces_const): Move from cli/cli-utils.c. * common/common-utils.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved from defs.h. (strtoulst): Move decl from utils.h. (hex2bin, bin2hex): Move decls from remote.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 * remote.c (hex2bin, bin2hex): Moved defs to common/common-utils.c. * remote.h (hex2bin, bin2hex): Moved decls 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. --- gdb/cli/cli-utils.c | 24 ------ gdb/cli/cli-utils.h | 9 --- gdb/common/common-utils.c | 187 +++++++++++++++++++++++++++++++++++++++++++++ gdb/common/common-utils.h | 34 +++++++++ gdb/defs.h | 19 ----- gdb/remote.c | 36 --------- gdb/remote.h | 4 - gdb/utils.c | 99 ------------------------ gdb/utils.h | 2 - 9 files changed, 221 insertions(+), 193 deletions(-) diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index f74e6b1..60f7553 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -213,30 +213,6 @@ number_is_in_list (char *list, int number) /* 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) { diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index 152fb89..2a8850d 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -89,15 +89,6 @@ extern int get_number_or_range (struct get_number_or_range_state *state); extern int number_is_in_list (char *list, int number); -/* 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. */ diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index 4204abf..5e96692 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -28,6 +28,7 @@ #include #include +#include /* The xmalloc() (libiberty.h) family of memory management routines. @@ -161,3 +162,189 @@ 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; +} + +/* Convert hex digit A to a number. */ + +static int +fromhex (int a) +{ + if (a >= '0' && a <= '9') + return a - '0'; + else if (a >= 'a' && a <= 'f') + return a - 'a' + 10; + else if (a >= 'A' && a <= 'F') + return a - 'A' + 10; + else + error (_("Reply contains invalid hex digit %d"), a); +} + +int +hex2bin (const char *hex, gdb_byte *bin, int count) +{ + int i; + + for (i = 0; i < count; i++) + { + if (hex[0] == 0 || hex[1] == 0) + { + /* Hex string is short, or of uneven length. + Return the count that has been converted so far. */ + return i; + } + *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); + hex += 2; + } + return i; +} + +/* Convert number NIB to a hex digit. */ + +static int +tohex (int nib) +{ + if (nib < 10) + return '0' + nib; + else + return 'a' + nib - 10; +} + +int +bin2hex (const gdb_byte *bin, char *hex, int count) +{ + int i; + + /* May use a length, or a nul-terminated string as input. */ + if (count == 0) + count = strlen ((char *) bin); + + for (i = 0; i < count; i++) + { + *hex++ = tohex ((*bin >> 4) & 0xf); + *hex++ = tohex (*bin++ & 0xf); + } + *hex = 0; + return i; +} + +/* 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; +} diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 9b659d8..ee7870e 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -25,6 +25,25 @@ #include #include +/* Static target-system-dependent parameters for GDB. */ + +/* Number of bits in a char or unsigned char for the target machine. + Just like CHAR_BIT in 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 ) 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 + extern void malloc_failure (long size) ATTRIBUTE_NORETURN; extern void internal_error (const char *file, int line, const char *, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 4); @@ -53,4 +72,19 @@ int xsnprintf (char *str, size_t size, const char *format, ...) char *savestring (const char *ptr, size_t len); +ULONGEST strtoulst (const char *num, const char **trailer, int base); + +extern int hex2bin (const char *hex, gdb_byte *bin, int count); + +extern int bin2hex (const gdb_byte *bin, char *hex, int count); + +/* 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); + #endif diff --git a/gdb/defs.h b/gdb/defs.h index d8a1adb..ec7e4f3 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -648,25 +648,6 @@ extern void *alloca (); enum { MAX_REGISTER_SIZE = 64 }; -/* Static target-system-dependent parameters for GDB. */ - -/* Number of bits in a char or unsigned char for the target machine. - Just like CHAR_BIT in 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 ) 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 - /* In findvar.c. */ extern LONGEST extract_signed_integer (const gdb_byte *, int, diff --git a/gdb/remote.c b/gdb/remote.c index aefbcf1..6a11652 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -4569,25 +4569,6 @@ fromhex (int a) error (_("Reply contains invalid hex digit %d"), a); } -int -hex2bin (const char *hex, gdb_byte *bin, int count) -{ - int i; - - for (i = 0; i < count; i++) - { - if (hex[0] == 0 || hex[1] == 0) - { - /* Hex string is short, or of uneven length. - Return the count that has been converted so far. */ - return i; - } - *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); - hex += 2; - } - return i; -} - /* Convert number NIB to a hex digit. */ static int @@ -4599,23 +4580,6 @@ tohex (int nib) return 'a' + nib - 10; } -int -bin2hex (const gdb_byte *bin, char *hex, int count) -{ - int i; - - /* May use a length, or a nul-terminated string as input. */ - if (count == 0) - count = strlen ((char *) bin); - - for (i = 0; i < count; i++) - { - *hex++ = tohex ((*bin >> 4) & 0xf); - *hex++ = tohex (*bin++ & 0xf); - } - *hex = 0; - return i; -} /* Check for the availability of vCont. This function should also check the response. */ diff --git a/gdb/remote.h b/gdb/remote.h index b95370c..d49b427 100644 --- a/gdb/remote.h +++ b/gdb/remote.h @@ -39,10 +39,6 @@ extern void getpkt (char **buf, long *sizeof_buf, int forever); extern int putpkt (char *buf); -extern int hex2bin (const char *hex, gdb_byte *bin, int count); - -extern int bin2hex (const gdb_byte *bin, char *hex, int count); - extern char *unpack_varlen_hex (char *buff, ULONGEST *result); extern void async_remote_interrupt_twice (void *arg); diff --git a/gdb/utils.c b/gdb/utils.c index a222c59..e1ced2c 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3233,105 +3233,6 @@ dummy_obstack_deallocate (void *object, void *data) return; } -/* 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; -} - /* Simple, portable version of dirname that does not modify its argument. */ diff --git a/gdb/utils.h b/gdb/utils.h index ad5bea4..e3b6fbf 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -39,8 +39,6 @@ extern int streq (const char *, const char *); extern int subset_compare (char *, char *); -ULONGEST strtoulst (const char *num, const char **trailer, int base); - int compare_positive_ints (const void *ap, const void *bp); int compare_strings (const void *ap, const void *bp); -- 1.7.10.4 --------------060600060005020806000309-- From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2430 invoked by alias); 5 Apr 2013 13:03:09 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 2420 invoked by uid 89); 5 Apr 2013 13:03:08 -0000 X-Spam-SWARE-Status: No, score=-3.7 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,MISSING_HEADERS,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL,TW_XS autolearn=no version=3.3.1 Received: from na3sys009aog132.obsmtp.com (HELO na3sys009aog132.obsmtp.com) (74.125.149.250) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Fri, 05 Apr 2013 13:03:05 +0000 Received: from mx10.qnx.com ([209.226.137.110]) (using TLSv1) by na3sys009aob132.postini.com ([74.125.148.12]) with SMTP ID DSNKUV7Lh9wJG122WHLKsGV8bgkMlTZVm0hC@postini.com; Fri, 05 Apr 2013 06:03:05 PDT Received: by mx10.qnx.com (Postfix, from userid 500) id 26F2E20E48; Fri, 5 Apr 2013 09:03:00 -0400 (EDT) Received: from exhts.ott.qnx.com (exch1 [10.222.2.137]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mx10.qnx.com (Postfix) with ESMTPS id 65D8720E1B for ; Fri, 5 Apr 2013 09:02:59 -0400 (EDT) Received: from [10.222.96.215] (10.222.2.5) by EXCH1.ott.qnx.com (10.222.2.137) with Microsoft SMTP Server id 14.2.318.4; Fri, 5 Apr 2013 09:02:59 -0400 Message-ID: <515ECAAF.8030209@qnx.com> Date: Fri, 05 Apr 2013 13:06:00 -0000 From: Aleksandar Ristovski User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: gmane.comp.gdb.patches CC: Subject: Re: [patch 1/6] Move utility functions to common/ References: <51278984.3070208@qnx.com> <20130310210746.GB21130@host2.jankratochvil.net> <514C56BA.5050504@qnx.com> In-Reply-To: <514C56BA.5050504@qnx.com> Content-Type: multipart/mixed; boundary="------------060600060005020806000309" X-Virus-Found: No X-SW-Source: 2013-04/txt/msg00118.txt.bz2 Message-ID: <20130405130600.cwaeFBg_Fjj4LXaizE6OnJn0uJ4oQ1YX0iFZaUIGzaM@z> --------------060600060005020806000309 Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1443 Rebased to master e96bd93d436e464a532a7e1161e1d201c9fc50c7 On 13-03-22 09:03 AM, Aleksandar Ristovski wrote: > As per the subject... > > > ChangeLog: > > * cli/cli-utils.c (skip_spaces, skip_spaces_const): Move defs to > common/common-utils.c. > * cli/cli-utils.h (skip_spaces, skip_spaces_const): Move decls to > common/common-utils.h. > * common/common-utils.c (ctype.h): Include. > (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move > from utils.c. > (fromhex): Copy from remote.c. > (hex2bin): Move from remote.c. > (tohex): Copy from remote.c. > (bin2hex): Move from remote.c. > (skip_spaces, skip_spaces_const): Move from cli/cli-utils.c. > * common/common-utils.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved > from defs.h. > (strtoulst): Move decl from utils.h. > (hex2bin, bin2hex): Move decls from remote.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 > * remote.c (hex2bin, bin2hex): Moved defs to common/common-utils.c. > * remote.h (hex2bin, bin2hex): Moved decls 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. > > > > Thanks, > > Aleksandar > > --------------060600060005020806000309 Content-Type: text/x-patch; name="0001-Move-utility-functions-to-common.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Move-utility-functions-to-common.patch" Content-length: 14058 >From d2cb04f1e90e14d72a63fa23ec5bb6421cc9e682 Mon Sep 17 00:00:00 2001 From: Aleksandar Ristovski Date: Wed, 27 Mar 2013 09:39:05 -0400 Subject: [PATCH 1/8] Move utility functions to common/ * cli/cli-utils.c (skip_spaces, skip_spaces_const): Move defs to common/common-utils.c. * cli/cli-utils.h (skip_spaces, skip_spaces_const): Move decls to common/common-utils.h. * common/common-utils.c (ctype.h): Include. (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move from utils.c. (fromhex): Copy from remote.c. (hex2bin): Move from remote.c. (tohex): Copy from remote.c. (bin2hex): Move from remote.c. (skip_spaces, skip_spaces_const): Move from cli/cli-utils.c. * common/common-utils.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved from defs.h. (strtoulst): Move decl from utils.h. (hex2bin, bin2hex): Move decls from remote.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 * remote.c (hex2bin, bin2hex): Moved defs to common/common-utils.c. * remote.h (hex2bin, bin2hex): Moved decls 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. --- gdb/cli/cli-utils.c | 24 ------ gdb/cli/cli-utils.h | 9 --- gdb/common/common-utils.c | 187 +++++++++++++++++++++++++++++++++++++++++++++ gdb/common/common-utils.h | 34 +++++++++ gdb/defs.h | 19 ----- gdb/remote.c | 36 --------- gdb/remote.h | 4 - gdb/utils.c | 99 ------------------------ gdb/utils.h | 2 - 9 files changed, 221 insertions(+), 193 deletions(-) diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index f74e6b1..60f7553 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -213,30 +213,6 @@ number_is_in_list (char *list, int number) /* 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) { diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index 152fb89..2a8850d 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -89,15 +89,6 @@ extern int get_number_or_range (struct get_number_or_range_state *state); extern int number_is_in_list (char *list, int number); -/* 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. */ diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index 4204abf..5e96692 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -28,6 +28,7 @@ #include #include +#include /* The xmalloc() (libiberty.h) family of memory management routines. @@ -161,3 +162,189 @@ 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; +} + +/* Convert hex digit A to a number. */ + +static int +fromhex (int a) +{ + if (a >= '0' && a <= '9') + return a - '0'; + else if (a >= 'a' && a <= 'f') + return a - 'a' + 10; + else if (a >= 'A' && a <= 'F') + return a - 'A' + 10; + else + error (_("Reply contains invalid hex digit %d"), a); +} + +int +hex2bin (const char *hex, gdb_byte *bin, int count) +{ + int i; + + for (i = 0; i < count; i++) + { + if (hex[0] == 0 || hex[1] == 0) + { + /* Hex string is short, or of uneven length. + Return the count that has been converted so far. */ + return i; + } + *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); + hex += 2; + } + return i; +} + +/* Convert number NIB to a hex digit. */ + +static int +tohex (int nib) +{ + if (nib < 10) + return '0' + nib; + else + return 'a' + nib - 10; +} + +int +bin2hex (const gdb_byte *bin, char *hex, int count) +{ + int i; + + /* May use a length, or a nul-terminated string as input. */ + if (count == 0) + count = strlen ((char *) bin); + + for (i = 0; i < count; i++) + { + *hex++ = tohex ((*bin >> 4) & 0xf); + *hex++ = tohex (*bin++ & 0xf); + } + *hex = 0; + return i; +} + +/* 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; +} diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 9b659d8..ee7870e 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -25,6 +25,25 @@ #include #include +/* Static target-system-dependent parameters for GDB. */ + +/* Number of bits in a char or unsigned char for the target machine. + Just like CHAR_BIT in 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 ) 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 + extern void malloc_failure (long size) ATTRIBUTE_NORETURN; extern void internal_error (const char *file, int line, const char *, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 4); @@ -53,4 +72,19 @@ int xsnprintf (char *str, size_t size, const char *format, ...) char *savestring (const char *ptr, size_t len); +ULONGEST strtoulst (const char *num, const char **trailer, int base); + +extern int hex2bin (const char *hex, gdb_byte *bin, int count); + +extern int bin2hex (const gdb_byte *bin, char *hex, int count); + +/* 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); + #endif diff --git a/gdb/defs.h b/gdb/defs.h index d8a1adb..ec7e4f3 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -648,25 +648,6 @@ extern void *alloca (); enum { MAX_REGISTER_SIZE = 64 }; -/* Static target-system-dependent parameters for GDB. */ - -/* Number of bits in a char or unsigned char for the target machine. - Just like CHAR_BIT in 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 ) 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 - /* In findvar.c. */ extern LONGEST extract_signed_integer (const gdb_byte *, int, diff --git a/gdb/remote.c b/gdb/remote.c index aefbcf1..6a11652 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -4569,25 +4569,6 @@ fromhex (int a) error (_("Reply contains invalid hex digit %d"), a); } -int -hex2bin (const char *hex, gdb_byte *bin, int count) -{ - int i; - - for (i = 0; i < count; i++) - { - if (hex[0] == 0 || hex[1] == 0) - { - /* Hex string is short, or of uneven length. - Return the count that has been converted so far. */ - return i; - } - *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); - hex += 2; - } - return i; -} - /* Convert number NIB to a hex digit. */ static int @@ -4599,23 +4580,6 @@ tohex (int nib) return 'a' + nib - 10; } -int -bin2hex (const gdb_byte *bin, char *hex, int count) -{ - int i; - - /* May use a length, or a nul-terminated string as input. */ - if (count == 0) - count = strlen ((char *) bin); - - for (i = 0; i < count; i++) - { - *hex++ = tohex ((*bin >> 4) & 0xf); - *hex++ = tohex (*bin++ & 0xf); - } - *hex = 0; - return i; -} /* Check for the availability of vCont. This function should also check the response. */ diff --git a/gdb/remote.h b/gdb/remote.h index b95370c..d49b427 100644 --- a/gdb/remote.h +++ b/gdb/remote.h @@ -39,10 +39,6 @@ extern void getpkt (char **buf, long *sizeof_buf, int forever); extern int putpkt (char *buf); -extern int hex2bin (const char *hex, gdb_byte *bin, int count); - -extern int bin2hex (const gdb_byte *bin, char *hex, int count); - extern char *unpack_varlen_hex (char *buff, ULONGEST *result); extern void async_remote_interrupt_twice (void *arg); diff --git a/gdb/utils.c b/gdb/utils.c index a222c59..e1ced2c 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3233,105 +3233,6 @@ dummy_obstack_deallocate (void *object, void *data) return; } -/* 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; -} - /* Simple, portable version of dirname that does not modify its argument. */ diff --git a/gdb/utils.h b/gdb/utils.h index ad5bea4..e3b6fbf 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -39,8 +39,6 @@ extern int streq (const char *, const char *); extern int subset_compare (char *, char *); -ULONGEST strtoulst (const char *num, const char **trailer, int base); - int compare_positive_ints (const void *ap, const void *bp); int compare_strings (const void *ap, const void *bp); -- 1.7.10.4 --------------060600060005020806000309--