From patchwork Wed Mar 19 22:30:14 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kratochvil X-Patchwork-Id: 174 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx20.g.dreamhost.com (caibbdcaaahc.dreamhost.com [208.113.200.72]) by wilcox.dreamhost.com (Postfix) with ESMTP id 82EFB36010F for ; Wed, 19 Mar 2014 15:30:52 -0700 (PDT) Received: by homiemail-mx20.g.dreamhost.com (Postfix, from userid 14314964) id 3259D40F88DAE; Wed, 19 Mar 2014 15:30:52 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx20.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx20.g.dreamhost.com (Postfix) with ESMTPS id 08DD340F88D35 for ; Wed, 19 Mar 2014 15:30:51 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:from:to:cc:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; q=dns; s=default; b=eHzJ49c9+3DdXvIDZKl+hCFBiUt7iKX22WpbFvxRQud JLQUG9jcJKZLDzGvHbI9mirEcS+5UlwrggAbXr87w4ipKjR0BUomiOX2M/YBE4nX u9JvPLBfzd4OKq9JJ/sQITHNYOZhYT/KqaouxL0QVjOmxiwD8WvWDKHo1jWIoVwg = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:from:to:cc:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; s=default; bh=jN+3jEnXzI1RIzcClCtC6iIP03E=; b=gAkGH21zEIx06Fp6N p8y18nwaPzb6DWNB2FxpKsrO0RO55KA+5vrtOIgjZWqv68AkdbdO/cIGYHeTQ8Rk aXe1CtFsI3XP3dWyWkwBFXGsisTHhGhtSNj6bKMI7aWoWAGd6Xp5SS47qfvIeYbO yTdHrYg3srSjnDy4UTn5zOsujc= Received: (qmail 18224 invoked by alias); 19 Mar 2014 22:30:48 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 18215 invoked by uid 89); 19 Mar 2014 22:30:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.1 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 19 Mar 2014 22:30:46 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2JMUfPk024674 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 19 Mar 2014 18:30:41 -0400 Received: from host1.jankratochvil.net (ovpn-116-22.ams2.redhat.com [10.36.116.22]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s2JMUEla003956; Wed, 19 Mar 2014 18:30:38 -0400 Subject: [PATCH v5 1/8] Move utility functions to common/ From: Jan Kratochvil To: gdb-patches@sourceware.org Cc: Aleksandar Ristovski Date: Wed, 19 Mar 2014 23:30:14 +0100 Message-ID: <20140319223014.14668.89536.stgit@host1.jankratochvil.net> In-Reply-To: <20140319223004.14668.20989.stgit@host1.jankratochvil.net> References: <20140319223004.14668.20989.stgit@host1.jankratochvil.net> User-Agent: StGit/0.17-dirty MIME-Version: 1.0 X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in Hi, some parts of the former patch have been reimplemented in the meantime by other patches so this is only a part of the former cleanup. Jan gdb/ 2014-02-26 Aleksandar Ristovski #include #include +#include /* The xmalloc() (libiberty.h) family of memory management routines. @@ -161,3 +163,126 @@ 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; +} diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 063698d..97889d5 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -71,4 +71,15 @@ 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); + +/* 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/common/host-defs.h b/gdb/common/host-defs.h index e4acef0..71a7029 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 + +/* 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 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 + #ifdef __MSDOS__ # define CANT_FORK # define GLOBAL_CURDIR diff --git a/gdb/defs.h b/gdb/defs.h index 47da43a..1aa7ad3 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -633,25 +633,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/utils.c b/gdb/utils.c index 364470c..21186b8 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3026,105 +3026,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 d6df2ee..a949cd7 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -41,8 +41,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);