Move utility functions to common/
[external/binutils.git] / gdb / common / common-utils.c
1 /* Shared general utility routines for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "common-defs.h"
21 #include "host-defs.h"
22 #include <ctype.h>
23
24 /* The xmalloc() (libiberty.h) family of memory management routines.
25
26    These are like the ISO-C malloc() family except that they implement
27    consistent semantics and guard against typical memory management
28    problems.  */
29
30 /* NOTE: These are declared using PTR to ensure consistency with
31    "libiberty.h".  xfree() is GDB local.  */
32
33 PTR                            /* ARI: PTR */
34 xmalloc (size_t size)
35 {
36   void *val;
37
38   /* See libiberty/xmalloc.c.  This function need's to match that's
39      semantics.  It never returns NULL.  */
40   if (size == 0)
41     size = 1;
42
43   val = malloc (size);         /* ARI: malloc */
44   if (val == NULL)
45     malloc_failure (size);
46
47   return val;
48 }
49
50 PTR                              /* ARI: PTR */
51 xrealloc (PTR ptr, size_t size)          /* ARI: PTR */
52 {
53   void *val;
54
55   /* See libiberty/xmalloc.c.  This function need's to match that's
56      semantics.  It never returns NULL.  */
57   if (size == 0)
58     size = 1;
59
60   if (ptr != NULL)
61     val = realloc (ptr, size);  /* ARI: realloc */
62   else
63     val = malloc (size);                /* ARI: malloc */
64   if (val == NULL)
65     malloc_failure (size);
66
67   return val;
68 }
69
70 PTR                            /* ARI: PTR */           
71 xcalloc (size_t number, size_t size)
72 {
73   void *mem;
74
75   /* See libiberty/xmalloc.c.  This function need's to match that's
76      semantics.  It never returns NULL.  */
77   if (number == 0 || size == 0)
78     {
79       number = 1;
80       size = 1;
81     }
82
83   mem = calloc (number, size);      /* ARI: xcalloc */
84   if (mem == NULL)
85     malloc_failure (number * size);
86
87   return mem;
88 }
89
90 void *
91 xzalloc (size_t size)
92 {
93   return xcalloc (1, size);
94 }
95
96 void
97 xfree (void *ptr)
98 {
99   if (ptr != NULL)
100     free (ptr);         /* ARI: free */
101 }
102
103 /* Like asprintf/vasprintf but get an internal_error if the call
104    fails. */
105
106 char *
107 xstrprintf (const char *format, ...)
108 {
109   char *ret;
110   va_list args;
111
112   va_start (args, format);
113   ret = xstrvprintf (format, args);
114   va_end (args);
115   return ret;
116 }
117
118 char *
119 xstrvprintf (const char *format, va_list ap)
120 {
121   char *ret = NULL;
122   int status = vasprintf (&ret, format, ap);
123
124   /* NULL is returned when there was a memory allocation problem, or
125      any other error (for instance, a bad format string).  A negative
126      status (the printed length) with a non-NULL buffer should never
127      happen, but just to be sure.  */
128   if (ret == NULL || status < 0)
129     internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
130   return ret;
131 }
132
133 int
134 xsnprintf (char *str, size_t size, const char *format, ...)
135 {
136   va_list args;
137   int ret;
138
139   va_start (args, format);
140   ret = vsnprintf (str, size, format, args);
141   gdb_assert (ret < size);
142   va_end (args);
143
144   return ret;
145 }
146
147 char *
148 savestring (const char *ptr, size_t len)
149 {
150   char *p = (char *) xmalloc (len + 1);
151
152   memcpy (p, ptr, len);
153   p[len] = 0;
154   return p;
155 }
156
157 /* The bit offset of the highest byte in a ULONGEST, for overflow
158    checking.  */
159
160 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
161
162 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
163    where 2 <= BASE <= 36.  */
164
165 static int
166 is_digit_in_base (unsigned char digit, int base)
167 {
168   if (!isalnum (digit))
169     return 0;
170   if (base <= 10)
171     return (isdigit (digit) && digit < base + '0');
172   else
173     return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
174 }
175
176 static int
177 digit_to_int (unsigned char c)
178 {
179   if (isdigit (c))
180     return c - '0';
181   else
182     return tolower (c) - 'a' + 10;
183 }
184
185 /* As for strtoul, but for ULONGEST results.  */
186
187 ULONGEST
188 strtoulst (const char *num, const char **trailer, int base)
189 {
190   unsigned int high_part;
191   ULONGEST result;
192   int minus = 0;
193   int i = 0;
194
195   /* Skip leading whitespace.  */
196   while (isspace (num[i]))
197     i++;
198
199   /* Handle prefixes.  */
200   if (num[i] == '+')
201     i++;
202   else if (num[i] == '-')
203     {
204       minus = 1;
205       i++;
206     }
207
208   if (base == 0 || base == 16)
209     {
210       if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
211         {
212           i += 2;
213           if (base == 0)
214             base = 16;
215         }
216     }
217
218   if (base == 0 && num[i] == '0')
219     base = 8;
220
221   if (base == 0)
222     base = 10;
223
224   if (base < 2 || base > 36)
225     {
226       errno = EINVAL;
227       return 0;
228     }
229
230   result = high_part = 0;
231   for (; is_digit_in_base (num[i], base); i += 1)
232     {
233       result = result * base + digit_to_int (num[i]);
234       high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
235       result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
236       if (high_part > 0xff)
237         {
238           errno = ERANGE;
239           result = ~ (ULONGEST) 0;
240           high_part = 0;
241           minus = 0;
242           break;
243         }
244     }
245
246   if (trailer != NULL)
247     *trailer = &num[i];
248
249   result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
250   if (minus)
251     return -result;
252   else
253     return result;
254 }
255
256 /* See documentation in cli-utils.h.  */
257
258 char *
259 skip_spaces (char *chp)
260 {
261   if (chp == NULL)
262     return NULL;
263   while (*chp && isspace (*chp))
264     chp++;
265   return chp;
266 }
267
268 /* A const-correct version of the above.  */
269
270 const char *
271 skip_spaces_const (const char *chp)
272 {
273   if (chp == NULL)
274     return NULL;
275   while (*chp && isspace (*chp))
276     chp++;
277   return chp;
278 }
279
280 /* See documentation in cli-utils.h.  */
281
282 const char *
283 skip_to_space_const (const char *chp)
284 {
285   if (chp == NULL)
286     return NULL;
287   while (*chp && !isspace (*chp))
288     chp++;
289   return chp;
290 }