1 /* Parse a printf-style format string.
3 Copyright (C) 1986-2013 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/>. */
31 parse_format_string (const char **arg)
35 const char *prev_start;
36 const char *percent_loc;
37 char *sub_start, *current_substring;
38 struct format_piece *pieces;
41 enum argclass this_argclass;
45 /* Parse the format-control string and copy it into the string STRING,
46 processing some kinds of escape sequence. */
48 f = string = (char *) alloca (strlen (s) + 1);
50 while (*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. */
107 /* Need extra space for the '\0's. Doubling the size is sufficient. */
109 current_substring = xmalloc (strlen (string) * 2 + 1000);
111 max_pieces = strlen (string) + 2;
113 pieces = (struct format_piece *)
114 xmalloc (max_pieces * sizeof (struct format_piece));
118 /* Now scan the string for %-specs and see what kinds of args they want.
119 argclass classifies the %-specs so we can give printf-type functions
120 something of the right size. */
127 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
128 int seen_space = 0, seen_plus = 0;
129 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
130 int seen_big_d = 0, seen_double_big_d = 0;
133 /* Skip over "%%", it will become part of a literal piece. */
140 sub_start = current_substring;
142 strncpy (current_substring, prev_start, f - 1 - prev_start);
143 current_substring += f - 1 - prev_start;
144 *current_substring++ = '\0';
146 pieces[next_frag].string = sub_start;
147 pieces[next_frag].argclass = literal_piece;
152 /* Check the validity of the format specifier, and work
153 out what argument it expects. We only accept C89
154 format strings, with the exception of long long (which
157 /* The first part of a format specifier is a set of flag
159 while (strchr ("0-+ #", *f))
172 /* The next part of a format specifier is a width. */
173 while (strchr ("0123456789", *f))
176 /* The next part of a format specifier is a precision. */
181 while (strchr ("0123456789", *f))
185 /* The next part of a format specifier is a length modifier. */
206 /* Decimal32 modifier. */
212 /* Decimal64 and Decimal128 modifiers. */
217 /* Check for a Decimal128. */
221 seen_double_big_d = 1;
237 if (seen_space || seen_plus)
244 this_argclass = int_arg;
245 else if (lcount == 1)
246 this_argclass = long_arg;
248 this_argclass = long_long_arg;
255 this_argclass = lcount == 0 ? int_arg : wide_char_arg;
256 if (lcount > 1 || seen_h || seen_big_l)
258 if (seen_prec || seen_zero || seen_space || seen_plus)
263 this_argclass = ptr_arg;
264 if (lcount || seen_h || seen_big_l)
266 if (seen_prec || seen_zero || seen_space || seen_plus)
271 this_argclass = lcount == 0 ? string_arg : wide_string_arg;
272 if (lcount > 1 || seen_h || seen_big_l)
274 if (seen_zero || seen_space || seen_plus)
283 if (seen_big_h || seen_big_d || seen_double_big_d)
284 this_argclass = decfloat_arg;
286 this_argclass = long_double_arg;
288 this_argclass = double_arg;
290 if (lcount || seen_h)
295 error (_("`*' not supported for precision or width in printf"));
298 error (_("Format specifier `n' not supported in printf"));
301 error (_("Incomplete format specifier at end of format string"));
304 error (_("Unrecognized format specifier '%c' in printf"), *f);
308 error (_("Inappropriate modifiers to "
309 "format specifier '%c' in printf"),
314 sub_start = current_substring;
316 if (lcount > 1 && USE_PRINTF_I64)
318 /* Windows' printf does support long long, but not the usual way.
319 Convert %lld to %I64d. */
320 int length_before_ll = f - percent_loc - 1 - lcount;
322 strncpy (current_substring, percent_loc, length_before_ll);
323 strcpy (current_substring + length_before_ll, "I64");
324 current_substring[length_before_ll + 3] =
325 percent_loc[length_before_ll + lcount];
326 current_substring += length_before_ll + 4;
328 else if (this_argclass == wide_string_arg
329 || this_argclass == wide_char_arg)
331 /* Convert %ls or %lc to %s. */
332 int length_before_ls = f - percent_loc - 2;
334 strncpy (current_substring, percent_loc, length_before_ls);
335 strcpy (current_substring + length_before_ls, "s");
336 current_substring += length_before_ls + 2;
340 strncpy (current_substring, percent_loc, f - percent_loc);
341 current_substring += f - percent_loc;
344 *current_substring++ = '\0';
348 pieces[next_frag].string = sub_start;
349 pieces[next_frag].argclass = this_argclass;
353 /* Record the remainder of the string. */
355 sub_start = current_substring;
357 strncpy (current_substring, prev_start, f - prev_start);
358 current_substring += f - prev_start;
359 *current_substring++ = '\0';
361 pieces[next_frag].string = sub_start;
362 pieces[next_frag].argclass = literal_piece;
365 /* Record an end-of-array marker. */
367 pieces[next_frag].string = NULL;
368 pieces[next_frag].argclass = literal_piece;
374 free_format_pieces (struct format_piece *pieces)
379 /* We happen to know that all the string pieces are in the block
380 pointed to by the first string piece. */
381 if (pieces[0].string)
382 xfree (pieces[0].string);
388 free_format_pieces_cleanup (void *ptr)
390 void **location = ptr;
392 if (location == NULL)
395 if (*location != NULL)
397 free_format_pieces (*location);