Remove init_cli_cmds
[deliverable/binutils-gdb.git] / gdb / gdbsupport / common-utils.c
CommitLineData
d26e3629
KY
1/* Shared general utility routines for GDB, the GNU debugger.
2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
d26e3629
KY
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
727605ca 20#include "common-defs.h"
2090129c 21#include "common-utils.h"
03aef70f
JK
22#include "host-defs.h"
23#include <ctype.h>
d26e3629 24
d26e3629
KY
25void *
26xzalloc (size_t size)
27{
28 return xcalloc (1, size);
29}
30
d26e3629
KY
31/* Like asprintf/vasprintf but get an internal_error if the call
32 fails. */
33
34char *
35xstrprintf (const char *format, ...)
36{
37 char *ret;
38 va_list args;
39
40 va_start (args, format);
41 ret = xstrvprintf (format, args);
42 va_end (args);
43 return ret;
44}
45
46char *
47xstrvprintf (const char *format, va_list ap)
48{
49 char *ret = NULL;
50 int status = vasprintf (&ret, format, ap);
51
52 /* NULL is returned when there was a memory allocation problem, or
53 any other error (for instance, a bad format string). A negative
54 status (the printed length) with a non-NULL buffer should never
55 happen, but just to be sure. */
56 if (ret == NULL || status < 0)
57 internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
58 return ret;
59}
60
d26e3629
KY
61int
62xsnprintf (char *str, size_t size, const char *format, ...)
63{
64 va_list args;
65 int ret;
66
67 va_start (args, format);
68 ret = vsnprintf (str, size, format, args);
69 gdb_assert (ret < size);
70 va_end (args);
71
72 return ret;
73}
baea0dae 74
d4081a38
PA
75/* See documentation in common-utils.h. */
76
77std::string
78string_printf (const char* fmt, ...)
79{
80 va_list vp;
81 int size;
82
83 va_start (vp, fmt);
84 size = vsnprintf (NULL, 0, fmt, vp);
85 va_end (vp);
86
87 std::string str (size, '\0');
88
89 /* C++11 and later guarantee std::string uses contiguous memory and
90 always includes the terminating '\0'. */
91 va_start (vp, fmt);
92 vsprintf (&str[0], fmt, vp);
93 va_end (vp);
94
95 return str;
96}
97
bd413795
TT
98/* See documentation in common-utils.h. */
99
100std::string
101string_vprintf (const char* fmt, va_list args)
102{
103 va_list vp;
104 size_t size;
105
106 va_copy (vp, args);
107 size = vsnprintf (NULL, 0, fmt, vp);
108 va_end (vp);
109
110 std::string str (size, '\0');
111
112 /* C++11 and later guarantee std::string uses contiguous memory and
113 always includes the terminating '\0'. */
114 vsprintf (&str[0], fmt, args);
115
116 return str;
117}
118
31b833b3
PA
119
120/* See documentation in common-utils.h. */
121
122void
123string_appendf (std::string &str, const char *fmt, ...)
124{
125 va_list vp;
126
127 va_start (vp, fmt);
128 string_vappendf (str, fmt, vp);
129 va_end (vp);
130}
131
132
133/* See documentation in common-utils.h. */
134
135void
136string_vappendf (std::string &str, const char *fmt, va_list args)
137{
138 va_list vp;
139 int grow_size;
140
141 va_copy (vp, args);
142 grow_size = vsnprintf (NULL, 0, fmt, vp);
143 va_end (vp);
144
145 size_t curr_size = str.size ();
146 str.resize (curr_size + grow_size);
147
148 /* C++11 and later guarantee std::string uses contiguous memory and
149 always includes the terminating '\0'. */
150 vsprintf (&str[curr_size], fmt, args);
151}
152
baea0dae
PA
153char *
154savestring (const char *ptr, size_t len)
155{
156 char *p = (char *) xmalloc (len + 1);
157
158 memcpy (p, ptr, len);
159 p[len] = 0;
160 return p;
161}
03aef70f
JK
162
163/* The bit offset of the highest byte in a ULONGEST, for overflow
164 checking. */
165
166#define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
167
168/* True (non-zero) iff DIGIT is a valid digit in radix BASE,
169 where 2 <= BASE <= 36. */
170
171static int
172is_digit_in_base (unsigned char digit, int base)
173{
174 if (!isalnum (digit))
175 return 0;
176 if (base <= 10)
177 return (isdigit (digit) && digit < base + '0');
178 else
179 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
180}
181
182static int
183digit_to_int (unsigned char c)
184{
185 if (isdigit (c))
186 return c - '0';
187 else
188 return tolower (c) - 'a' + 10;
189}
190
191/* As for strtoul, but for ULONGEST results. */
192
193ULONGEST
194strtoulst (const char *num, const char **trailer, int base)
195{
196 unsigned int high_part;
197 ULONGEST result;
198 int minus = 0;
199 int i = 0;
200
201 /* Skip leading whitespace. */
202 while (isspace (num[i]))
203 i++;
204
205 /* Handle prefixes. */
206 if (num[i] == '+')
207 i++;
208 else if (num[i] == '-')
209 {
210 minus = 1;
211 i++;
212 }
213
214 if (base == 0 || base == 16)
215 {
216 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
217 {
218 i += 2;
219 if (base == 0)
220 base = 16;
221 }
222 }
223
224 if (base == 0 && num[i] == '0')
225 base = 8;
226
227 if (base == 0)
228 base = 10;
229
230 if (base < 2 || base > 36)
231 {
232 errno = EINVAL;
233 return 0;
234 }
235
236 result = high_part = 0;
237 for (; is_digit_in_base (num[i], base); i += 1)
238 {
239 result = result * base + digit_to_int (num[i]);
240 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
241 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
242 if (high_part > 0xff)
243 {
244 errno = ERANGE;
245 result = ~ (ULONGEST) 0;
246 high_part = 0;
247 minus = 0;
248 break;
249 }
250 }
251
252 if (trailer != NULL)
253 *trailer = &num[i];
254
255 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
256 if (minus)
257 return -result;
258 else
259 return result;
260}
261
41548caa 262/* See documentation in common-utils.h. */
03aef70f
JK
263
264char *
265skip_spaces (char *chp)
266{
267 if (chp == NULL)
268 return NULL;
269 while (*chp && isspace (*chp))
270 chp++;
271 return chp;
272}
273
274/* A const-correct version of the above. */
275
276const char *
f1735a53 277skip_spaces (const char *chp)
03aef70f
JK
278{
279 if (chp == NULL)
280 return NULL;
281 while (*chp && isspace (*chp))
282 chp++;
283 return chp;
284}
285
41548caa 286/* See documentation in common-utils.h. */
03aef70f
JK
287
288const char *
f1735a53 289skip_to_space (const char *chp)
03aef70f
JK
290{
291 if (chp == NULL)
292 return NULL;
293 while (*chp && !isspace (*chp))
294 chp++;
295 return chp;
296}
7c5ded6a 297
f1735a53
TT
298/* See documentation in common-utils.h. */
299
300char *
301skip_to_space (char *chp)
302{
303 return (char *) skip_to_space ((const char *) chp);
304}
305
268a13a5 306/* See gdbsupport/common-utils.h. */
7c5ded6a
SDJ
307
308void
309free_vector_argv (std::vector<char *> &v)
310{
311 for (char *el : v)
312 xfree (el);
313
314 v.clear ();
315}
2090129c 316
268a13a5 317/* See gdbsupport/common-utils.h. */
2090129c
SDJ
318
319std::string
320stringify_argv (const std::vector<char *> &args)
321{
322 std::string ret;
323
2f91880f 324 if (!args.empty () && args[0] != NULL)
2090129c
SDJ
325 {
326 for (auto s : args)
327 if (s != NULL)
328 {
329 ret += s;
330 ret += ' ';
331 }
332
333 /* Erase the last whitespace. */
334 ret.erase (ret.end () - 1);
335 }
336
337 return ret;
338}
25e3c82c 339
268a13a5 340/* See gdbsupport/common-utils.h. */
25e3c82c 341
a3b60e45
JK
342ULONGEST
343align_up (ULONGEST v, int n)
344{
345 /* Check that N is really a power of two. */
346 gdb_assert (n && (n & (n-1)) == 0);
347 return (v + n - 1) & -n;
348}
349
268a13a5 350/* See gdbsupport/common-utils.h. */
a3b60e45
JK
351
352ULONGEST
353align_down (ULONGEST v, int n)
354{
355 /* Check that N is really a power of two. */
356 gdb_assert (n && (n & (n-1)) == 0);
357 return (v & -n);
358}
This page took 0.773234 seconds and 4 git commands to generate.