2 Copyright (C) 2001-2004, 2006-2007, 2009, 2015 Free Software
4 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "xvasprintf.h"
32 #define _(str) gettext (str)
34 #include "format-invalid.h"
36 #define INVALID_C99_MACRO(directive_number) \
37 xasprintf (_("In the directive number %u, the token after '<' is not the name of a format specifier macro. The valid macro names are listed in ISO C 99 section 7.8.1."), directive_number)
39 #define INVALID_ANGLE_BRACKET(directive_number) \
40 xasprintf (_("In the directive number %u, the token after '<' is not followed by '>'."), directive_number)
42 #define INVALID_IGNORED_ARGUMENT(referenced_arg, ignored_arg) \
43 xasprintf (_("The string refers to argument number %u but ignores argument number %u."), referenced_arg, ignored_arg)
45 /* Execute statement if memory allocation function returned NULL. */
46 #define IF_OOM(allocated_ptr, statement) /* nothing, since we use xalloc.h */
48 /* Specifies whether the system dependent segments in msgid and msgstr have
49 been processed. This means:
50 - If false, ISO C 99 <inttypes.h> directives are denoted with angle
51 brackets. If true, they have already been expanded, leading in
52 particular to %I64d directives on native Windows platforms.
53 - If false, the 'I' flag may be present in msgstr (also on platforms
54 other than glibc). If true, the 'I' directive may be present in msgstr
55 only on glibc >= 2.2 platforms. */
56 #define SYSDEP_SEGMENTS_PROCESSED false
58 /* Include the bulk of the C format string parsing code. */
59 #include "format-c-parse.h"
62 format_parse (const char *format, bool translated, bool objc_extensions,
63 char *fdi, char **invalid_reason)
65 struct spec result_buf;
68 result = format_parse_entrails (format, translated, objc_extensions, fdi, invalid_reason, &result_buf);
72 /* Copy the result to a heap-allocated object. */
73 struct spec *safe_result = XMALLOC (struct spec);
74 *safe_result = *result;
81 format_c_parse (const char *format, bool translated, char *fdi,
82 char **invalid_reason)
84 return format_parse (format, translated, false, fdi, invalid_reason);
88 format_objc_parse (const char *format, bool translated, char *fdi,
89 char **invalid_reason)
91 return format_parse (format, translated, true, fdi, invalid_reason);
95 format_free (void *descr)
97 struct spec *spec = (struct spec *) descr;
99 if (spec->unnumbered != NULL)
100 free (spec->unnumbered);
101 if (spec->sysdep_directives != NULL)
102 free (spec->sysdep_directives);
107 format_is_unlikely_intentional (void *descr)
109 struct spec *spec = (struct spec *) descr;
111 return spec->unlikely_intentional;
115 format_get_number_of_directives (void *descr)
117 struct spec *spec = (struct spec *) descr;
119 return spec->directives;
123 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
124 formatstring_error_logger_t error_logger,
125 const char *pretty_msgid, const char *pretty_msgstr)
127 struct spec *spec1 = (struct spec *) msgid_descr;
128 struct spec *spec2 = (struct spec *) msgstr_descr;
132 /* Check the argument types are the same. */
134 ? spec1->unnumbered_arg_count != spec2->unnumbered_arg_count
135 : spec1->unnumbered_arg_count < spec2->unnumbered_arg_count)
138 error_logger (_("number of format specifications in '%s' and '%s' does not match"),
139 pretty_msgid, pretty_msgstr);
143 for (i = 0; i < spec2->unnumbered_arg_count; i++)
144 if (spec1->unnumbered[i].type != spec2->unnumbered[i].type)
147 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
148 pretty_msgid, pretty_msgstr, i + 1);
156 struct formatstring_parser formatstring_c =
160 format_get_number_of_directives,
161 format_is_unlikely_intentional,
166 struct formatstring_parser formatstring_objc =
170 format_get_number_of_directives,
171 format_is_unlikely_intentional,
177 get_sysdep_c_format_directives (const char *string, bool translated,
178 struct interval **intervalsp, size_t *lengthp)
180 /* Parse the format string with all possible extensions turned on. (The
181 caller has already verified that the format string is valid for the
182 particular language.) */
183 char *invalid_reason = NULL;
186 format_parse (string, translated, true, NULL, &invalid_reason);
188 if (descr != NULL && descr->sysdep_directives_count > 0)
190 unsigned int n = descr->sysdep_directives_count;
191 struct interval *intervals = XNMALLOC (n, struct interval);
194 for (i = 0; i < n; i++)
196 intervals[i].startpos = descr->sysdep_directives[2 * i] - string;
197 intervals[i].endpos = descr->sysdep_directives[2 * i + 1] - string;
199 *intervalsp = intervals;
211 free (invalid_reason);
217 /* Test program: Print the argument list specification returned by
218 format_parse for strings read from standard input. */
223 format_print (void *descr)
225 struct spec *spec = (struct spec *) descr;
235 for (i = 0; i < spec->unnumbered_arg_count; i++)
239 if (spec->unnumbered[i].type & FAT_UNSIGNED)
240 printf ("[unsigned]");
241 switch (spec->unnumbered[i].type & FAT_SIZE_MASK)
254 case FAT_SIZE_LONGLONG:
255 printf ("[long long]");
261 printf ("[int16_t]");
264 printf ("[int32_t]");
267 printf ("[int64_t]");
269 case FAT_SIZE_LEAST8_T:
270 printf ("[int_least8_t]");
272 case FAT_SIZE_LEAST16_T:
273 printf ("[int_least16_t]");
275 case FAT_SIZE_LEAST32_T:
276 printf ("[int_least32_t]");
278 case FAT_SIZE_LEAST64_T:
279 printf ("[int_least64_t]");
281 case FAT_SIZE_FAST8_T:
282 printf ("[int_fast8_t]");
284 case FAT_SIZE_FAST16_T:
285 printf ("[int_fast16_t]");
287 case FAT_SIZE_FAST32_T:
288 printf ("[int_fast32_t]");
290 case FAT_SIZE_FAST64_T:
291 printf ("[int_fast64_t]");
293 case FAT_SIZE_INTMAX_T:
294 printf ("[intmax_t]");
296 case FAT_SIZE_INTPTR_T:
297 printf ("[intptr_t]");
299 case FAT_SIZE_SIZE_T:
302 case FAT_SIZE_PTRDIFF_T:
303 printf ("[ptrdiff_t]");
308 switch (spec->unnumbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_MASK))
322 case FAT_OBJC_OBJECT:
328 case FAT_COUNT_POINTER:
344 size_t line_size = 0;
346 char *invalid_reason;
349 line_len = getline (&line, &line_size, stdin);
352 if (line_len > 0 && line[line_len - 1] == '\n')
353 line[--line_len] = '\0';
355 invalid_reason = NULL;
356 descr = format_c_parse (line, false, NULL, &invalid_reason);
358 format_print (descr);
361 printf ("%s\n", invalid_reason);
363 free (invalid_reason);
371 * For Emacs M-x compile
373 * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../intl -DHAVE_CONFIG_H -DTEST format-c.c ../gnulib-lib/libgettextlib.la"