1 /* Parse a printf-style format string.
3 Copyright (C) 1986-2012 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 (char **arg
)
36 char *sub_start
, *current_substring
;
37 struct format_piece
*pieces
;
40 enum argclass this_argclass
;
44 /* Parse the format-control string and copy it into the string STRING,
45 processing some kinds of escape sequence. */
47 f
= string
= (char *) alloca (strlen (s
) + 1);
49 while (*s
!= '"' && *s
!= '\0')
88 /* ??? TODO: handle other escape sequences. */
89 error (_("Unrecognized escape character \\%c in format string."),
99 /* Terminate our escape-processed copy. */
102 /* Whether the format string ended with double-quote or zero, we're
103 done with it; it's up to callers to complain about syntax. */
106 /* Need extra space for the '\0's. Doubling the size is sufficient. */
108 current_substring
= xmalloc (strlen (string
) * 2 + 1000);
110 max_pieces
= strlen (string
) + 2;
112 pieces
= (struct format_piece
*)
113 xmalloc (max_pieces
* sizeof (struct format_piece
));
117 /* Now scan the string for %-specs and see what kinds of args they want.
118 argclass classifies the %-specs so we can give printf-type functions
119 something of the right size. */
126 int seen_hash
= 0, seen_zero
= 0, lcount
= 0, seen_prec
= 0;
127 int seen_space
= 0, seen_plus
= 0;
128 int seen_big_l
= 0, seen_h
= 0, seen_big_h
= 0;
129 int seen_big_d
= 0, seen_double_big_d
= 0;
132 /* Skip over "%%", it will become part of a literal piece. */
139 sub_start
= current_substring
;
141 strncpy (current_substring
, prev_start
, f
- 1 - prev_start
);
142 current_substring
+= f
- 1 - prev_start
;
143 *current_substring
++ = '\0';
145 pieces
[next_frag
].string
= sub_start
;
146 pieces
[next_frag
].argclass
= literal_piece
;
151 /* Check the validity of the format specifier, and work
152 out what argument it expects. We only accept C89
153 format strings, with the exception of long long (which
156 /* The first part of a format specifier is a set of flag
158 while (strchr ("0-+ #", *f
))
171 /* The next part of a format specifier is a width. */
172 while (strchr ("0123456789", *f
))
175 /* The next part of a format specifier is a precision. */
180 while (strchr ("0123456789", *f
))
184 /* The next part of a format specifier is a length modifier. */
205 /* Decimal32 modifier. */
211 /* Decimal64 and Decimal128 modifiers. */
216 /* Check for a Decimal128. */
220 seen_double_big_d
= 1;
236 if (seen_space
|| seen_plus
)
243 this_argclass
= int_arg
;
244 else if (lcount
== 1)
245 this_argclass
= long_arg
;
247 this_argclass
= long_long_arg
;
254 this_argclass
= lcount
== 0 ? int_arg
: wide_char_arg
;
255 if (lcount
> 1 || seen_h
|| seen_big_l
)
257 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
262 this_argclass
= ptr_arg
;
263 if (lcount
|| seen_h
|| seen_big_l
)
265 if (seen_prec
|| seen_zero
|| seen_space
|| seen_plus
)
270 this_argclass
= lcount
== 0 ? string_arg
: wide_string_arg
;
271 if (lcount
> 1 || seen_h
|| seen_big_l
)
273 if (seen_zero
|| seen_space
|| seen_plus
)
282 if (seen_big_h
|| seen_big_d
|| seen_double_big_d
)
283 this_argclass
= decfloat_arg
;
285 this_argclass
= long_double_arg
;
287 this_argclass
= double_arg
;
289 if (lcount
|| seen_h
)
294 error (_("`*' not supported for precision or width in printf"));
297 error (_("Format specifier `n' not supported in printf"));
300 error (_("Incomplete format specifier at end of format string"));
303 error (_("Unrecognized format specifier '%c' in printf"), *f
);
307 error (_("Inappropriate modifiers to "
308 "format specifier '%c' in printf"),
313 sub_start
= current_substring
;
315 if (lcount
> 1 && USE_PRINTF_I64
)
317 /* Windows' printf does support long long, but not the usual way.
318 Convert %lld to %I64d. */
319 int length_before_ll
= f
- percent_loc
- 1 - lcount
;
321 strncpy (current_substring
, percent_loc
, length_before_ll
);
322 strcpy (current_substring
+ length_before_ll
, "I64");
323 current_substring
[length_before_ll
+ 3] =
324 percent_loc
[length_before_ll
+ lcount
];
325 current_substring
+= length_before_ll
+ 4;
327 else if (this_argclass
== wide_string_arg
328 || this_argclass
== wide_char_arg
)
330 /* Convert %ls or %lc to %s. */
331 int length_before_ls
= f
- percent_loc
- 2;
333 strncpy (current_substring
, percent_loc
, length_before_ls
);
334 strcpy (current_substring
+ length_before_ls
, "s");
335 current_substring
+= length_before_ls
+ 2;
339 strncpy (current_substring
, percent_loc
, f
- percent_loc
);
340 current_substring
+= f
- percent_loc
;
343 *current_substring
++ = '\0';
347 pieces
[next_frag
].string
= sub_start
;
348 pieces
[next_frag
].argclass
= this_argclass
;
352 /* Record the remainder of the string. */
354 sub_start
= current_substring
;
356 strncpy (current_substring
, prev_start
, f
- prev_start
);
357 current_substring
+= f
- prev_start
;
358 *current_substring
++ = '\0';
360 pieces
[next_frag
].string
= sub_start
;
361 pieces
[next_frag
].argclass
= literal_piece
;
364 /* Record an end-of-array marker. */
366 pieces
[next_frag
].string
= NULL
;
367 pieces
[next_frag
].argclass
= literal_piece
;
373 free_format_pieces (struct format_piece
*pieces
)
378 /* We happen to know that all the string pieces are in the block
379 pointed to by the first string piece. */
380 if (pieces
[0].string
)
381 xfree (pieces
[0].string
);
387 free_format_pieces_cleanup (void *ptr
)
389 void **location
= ptr
;
391 if (location
== NULL
)
394 if (*location
!= NULL
)
396 free_format_pieces (*location
);
This page took 0.038597 seconds and 4 git commands to generate.