1 /* Parse a printf-style format string.
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "common-defs.h"
23 format_pieces::format_pieces (const char **arg
, bool gdb_extensions
)
27 const char *prev_start
;
28 const char *percent_loc
;
29 char *sub_start
, *current_substring
;
30 enum argclass this_argclass
;
37 *arg
+= strlen (*arg
);
41 /* Parse the format-control string and copy it into the string STRING,
42 processing some kinds of escape sequence. */
44 char *f
= (char *) alloca (strlen (s
) + 1);
47 while ((gdb_extensions
|| *s
!= '"') && *s
!= '\0')
89 /* ??? TODO: handle other escape sequences. */
90 error (_("Unrecognized escape character \\%c in format string."),
100 /* Terminate our escape-processed copy. */
103 /* Whether the format string ended with double-quote or zero, we're
104 done with it; it's up to callers to complain about syntax. */
108 /* Need extra space for the '\0's. Doubling the size is sufficient. */
110 current_substring
= (char *) xmalloc (strlen (string
) * 2 + 1000);
111 m_storage
.reset (current_substring
);
113 /* Now scan the string for %-specs and see what kinds of args they want.
114 argclass classifies the %-specs so we can give printf-type functions
115 something of the right size. */
117 const char *f
= string
;
122 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
123 int seen_space
= 0, seen_plus
= 0;
124 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
125 int seen_big_d
= 0, seen_double_big_d
= 0;
129 /* Skip over "%%", it will become part of a literal piece. */
136 sub_start
= current_substring
;
138 strncpy (current_substring
, prev_start
, f
- 1 - prev_start
);
139 current_substring
+= f
- 1 - prev_start
;
140 *current_substring
++ = '\0';
142 if (*sub_start
!= '\0')
143 m_pieces
.emplace_back (sub_start
, literal_piece
, 0);
147 /* Check the validity of the format specifier, and work
148 out what argument it expects. We only accept C89
149 format strings, with the exception of long long (which
152 /* The first part of a format specifier is a set of flag
154 while (*f
!= '\0' && strchr ("0-+ #", *f
))
167 /* The next part of a format specifier is a width. */
168 if (gdb_extensions
&& *f
== '*')
175 while (*f
!= '\0' && strchr ("0123456789", *f
))
179 /* The next part of a format specifier is a precision. */
184 if (gdb_extensions
&& *f
== '*')
191 while (*f
!= '\0' && strchr ("0123456789", *f
))
196 /* The next part of a format specifier is a length modifier. */
217 /* Decimal32 modifier. */
223 /* Decimal64 and Decimal128 modifiers. */
228 /* Check for a Decimal128. */
232 seen_double_big_d
= 1;
248 if (seen_space
|| seen_plus
)
255 this_argclass
= int_arg
;
256 else if (lcount
== 1)
257 this_argclass
= long_arg
;
259 this_argclass
= long_long_arg
;
266 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
267 if (lcount
> 1 || seen_h
|| seen_big_l
)
269 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
274 this_argclass
= ptr_arg
;
275 if (lcount
|| seen_h
|| seen_big_l
)
279 if (seen_hash
|| seen_zero
|| seen_space
|| seen_plus
)
298 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
299 if (lcount
> 1 || seen_h
|| seen_big_l
)
301 if (seen_zero
|| seen_space
|| seen_plus
)
310 if (seen_double_big_d
)
311 this_argclass
= dec128float_arg
;
313 this_argclass
= dec64float_arg
;
315 this_argclass
= dec32float_arg
;
317 this_argclass
= long_double_arg
;
319 this_argclass
= double_arg
;
321 if (lcount
|| seen_h
)
326 error (_("`*' not supported for precision or width in printf"));
329 error (_("Format specifier `n' not supported in printf"));
332 error (_("Incomplete format specifier at end of format string"));
335 error (_("Unrecognized format specifier '%c' in printf"), *f
);
339 error (_("Inappropriate modifiers to "
340 "format specifier '%c' in printf"),
345 sub_start
= current_substring
;
347 if (lcount
> 1 && USE_PRINTF_I64
)
349 /* Windows' printf does support long long, but not the usual way.
350 Convert %lld to %I64d. */
351 int length_before_ll
= f
- percent_loc
- 1 - lcount
;
353 strncpy (current_substring
, percent_loc
, length_before_ll
);
354 strcpy (current_substring
+ length_before_ll
, "I64");
355 current_substring
[length_before_ll
+ 3] =
356 percent_loc
[length_before_ll
+ lcount
];
357 current_substring
+= length_before_ll
+ 4;
359 else if (this_argclass
== wide_string_arg
360 || this_argclass
== wide_char_arg
)
362 /* Convert %ls or %lc to %s. */
363 int length_before_ls
= f
- percent_loc
- 2;
365 strncpy (current_substring
, percent_loc
, length_before_ls
);
366 strcpy (current_substring
+ length_before_ls
, "s");
367 current_substring
+= length_before_ls
+ 2;
371 strncpy (current_substring
, percent_loc
, f
- percent_loc
);
372 current_substring
+= f
- percent_loc
;
375 *current_substring
++ = '\0';
379 m_pieces
.emplace_back (sub_start
, this_argclass
, n_int_args
);
382 /* Record the remainder of the string. */
386 sub_start
= current_substring
;
388 strncpy (current_substring
, prev_start
, f
- prev_start
);
389 current_substring
+= f
- prev_start
;
390 *current_substring
++ = '\0';
392 m_pieces
.emplace_back (sub_start
, literal_piece
, 0);
This page took 0.046367 seconds and 5 git commands to generate.