1 /* Parse a printf-style format string.
3 Copyright (C) 1986-2014 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 (*f
!= '\0' && strchr ("0-+ #", *f
))
172 /* The next part of a format specifier is a width. */
173 while (*f
!= '\0' && strchr ("0123456789", *f
))
176 /* The next part of a format specifier is a precision. */
181 while (*f
!= '\0' && 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
)
268 if (seen_hash
|| seen_zero
|| seen_space
|| seen_plus
)
273 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
274 if (lcount
> 1 || seen_h
|| seen_big_l
)
276 if (seen_zero
|| seen_space
|| seen_plus
)
285 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
286 this_argclass
= decfloat_arg
;
288 this_argclass
= long_double_arg
;
290 this_argclass
= double_arg
;
292 if (lcount
|| seen_h
)
297 error (_("`*' not supported for precision or width in printf"));
300 error (_("Format specifier `n' not supported in printf"));
303 error (_("Incomplete format specifier at end of format string"));
306 error (_("Unrecognized format specifier '%c' in printf"), *f
);
310 error (_("Inappropriate modifiers to "
311 "format specifier '%c' in printf"),
316 sub_start
= current_substring
;
318 if (lcount
> 1 && USE_PRINTF_I64
)
320 /* Windows' printf does support long long, but not the usual way.
321 Convert %lld to %I64d. */
322 int length_before_ll
= f
- percent_loc
- 1 - lcount
;
324 strncpy (current_substring
, percent_loc
, length_before_ll
);
325 strcpy (current_substring
+ length_before_ll
, "I64");
326 current_substring
[length_before_ll
+ 3] =
327 percent_loc
[length_before_ll
+ lcount
];
328 current_substring
+= length_before_ll
+ 4;
330 else if (this_argclass
== wide_string_arg
331 || this_argclass
== wide_char_arg
)
333 /* Convert %ls or %lc to %s. */
334 int length_before_ls
= f
- percent_loc
- 2;
336 strncpy (current_substring
, percent_loc
, length_before_ls
);
337 strcpy (current_substring
+ length_before_ls
, "s");
338 current_substring
+= length_before_ls
+ 2;
342 strncpy (current_substring
, percent_loc
, f
- percent_loc
);
343 current_substring
+= f
- percent_loc
;
346 *current_substring
++ = '\0';
350 pieces
[next_frag
].string
= sub_start
;
351 pieces
[next_frag
].argclass
= this_argclass
;
355 /* Record the remainder of the string. */
357 sub_start
= current_substring
;
359 strncpy (current_substring
, prev_start
, f
- prev_start
);
360 current_substring
+= f
- prev_start
;
361 *current_substring
++ = '\0';
363 pieces
[next_frag
].string
= sub_start
;
364 pieces
[next_frag
].argclass
= literal_piece
;
367 /* Record an end-of-array marker. */
369 pieces
[next_frag
].string
= NULL
;
370 pieces
[next_frag
].argclass
= literal_piece
;
376 free_format_pieces (struct format_piece
*pieces
)
381 /* We happen to know that all the string pieces are in the block
382 pointed to by the first string piece. */
383 if (pieces
[0].string
)
384 xfree (pieces
[0].string
);
390 free_format_pieces_cleanup (void *ptr
)
392 void **location
= ptr
;
394 if (location
== NULL
)
397 if (*location
!= NULL
)
399 free_format_pieces (*location
);
This page took 0.038129 seconds and 4 git commands to generate.