2 Copyright (C) 2001-2004, 2006-2007, 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "xvasprintf.h"
31 #define _(str) gettext (str)
33 #include "format-invalid.h"
35 #define INVALID_C99_MACRO(directive_number) \
36 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)
38 #define INVALID_ANGLE_BRACKET(directive_number) \
39 xasprintf (_("In the directive number %u, the token after '<' is not followed by '>'."), directive_number)
41 #define INVALID_IGNORED_ARGUMENT(referenced_arg, ignored_arg) \
42 xasprintf (_("The string refers to argument number %u but ignores argument number %u."), referenced_arg, ignored_arg)
44 /* Execute statement if memory allocation function returned NULL. */
45 #define IF_OOM(allocated_ptr, statement) /* nothing, since we use xalloc.h */
47 /* Specifies whether the system dependent segments in msgid and msgstr have
48 been processed. This means:
49 - If false, ISO C 99 <inttypes.h> directives are denoted with angle
50 brackets. If true, they have already been expanded, leading in
51 particular to %I64d directives on native Windows platforms.
52 - If false, the 'I' flag may be present in msgstr (also on platforms
53 other than glibc). If true, the 'I' directive may be present in msgstr
54 only on glibc >= 2.2 platforms. */
55 #define SYSDEP_SEGMENTS_PROCESSED false
57 /* Include the bulk of the C format string parsing code. */
58 #include "format-c-parse.h"
61 format_parse (const char *format, bool translated, bool objc_extensions,
62 char *fdi, char **invalid_reason)
64 struct spec result_buf;
67 result = format_parse_entrails (format, translated, objc_extensions, fdi, invalid_reason, &result_buf);
71 /* Copy the result to a heap-allocated object. */
72 struct spec *safe_result = XMALLOC (struct spec);
73 *safe_result = *result;
80 format_c_parse (const char *format, bool translated, char *fdi,
81 char **invalid_reason)
83 return format_parse (format, translated, false, fdi, invalid_reason);
87 format_objc_parse (const char *format, bool translated, char *fdi,
88 char **invalid_reason)
90 return format_parse (format, translated, true, fdi, invalid_reason);
94 format_free (void *descr)
96 struct spec *spec = (struct spec *) descr;
98 if (spec->unnumbered != NULL)
99 free (spec->unnumbered);
100 if (spec->sysdep_directives != NULL)
101 free (spec->sysdep_directives);
106 format_is_unlikely_intentional (void *descr)
108 struct spec *spec = (struct spec *) descr;
110 return spec->unlikely_intentional;
114 format_get_number_of_directives (void *descr)
116 struct spec *spec = (struct spec *) descr;
118 return spec->directives;
122 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
123 formatstring_error_logger_t error_logger,
124 const char *pretty_msgid, const char *pretty_msgstr)
126 struct spec *spec1 = (struct spec *) msgid_descr;
127 struct spec *spec2 = (struct spec *) msgstr_descr;
131 /* Check the argument types are the same. */
133 ? spec1->unnumbered_arg_count != spec2->unnumbered_arg_count
134 : spec1->unnumbered_arg_count < spec2->unnumbered_arg_count)
137 error_logger (_("number of format specifications in '%s' and '%s' does not match"),
138 pretty_msgid, pretty_msgstr);
142 for (i = 0; i < spec2->unnumbered_arg_count; i++)
143 if (spec1->unnumbered[i].type != spec2->unnumbered[i].type)
146 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
147 pretty_msgid, pretty_msgstr, i + 1);
155 struct formatstring_parser formatstring_c =
159 format_get_number_of_directives,
160 format_is_unlikely_intentional,
165 struct formatstring_parser formatstring_objc =
169 format_get_number_of_directives,
170 format_is_unlikely_intentional,
176 get_sysdep_c_format_directives (const char *string, bool translated,
177 struct interval **intervalsp, size_t *lengthp)
179 /* Parse the format string with all possible extensions turned on. (The
180 caller has already verified that the format string is valid for the
181 particular language.) */
182 char *invalid_reason = NULL;
185 format_parse (string, translated, true, NULL, &invalid_reason);
187 if (descr != NULL && descr->sysdep_directives_count > 0)
189 unsigned int n = descr->sysdep_directives_count;
190 struct interval *intervals = XNMALLOC (n, struct interval);
193 for (i = 0; i < n; i++)
195 intervals[i].startpos = descr->sysdep_directives[2 * i] - string;
196 intervals[i].endpos = descr->sysdep_directives[2 * i + 1] - string;
198 *intervalsp = intervals;
210 free (invalid_reason);
216 /* Test program: Print the argument list specification returned by
217 format_parse for strings read from standard input. */
222 format_print (void *descr)
224 struct spec *spec = (struct spec *) descr;
234 for (i = 0; i < spec->unnumbered_arg_count; i++)
238 if (spec->unnumbered[i].type & FAT_UNSIGNED)
239 printf ("[unsigned]");
240 switch (spec->unnumbered[i].type & FAT_SIZE_MASK)
253 case FAT_SIZE_LONGLONG:
254 printf ("[long long]");
260 printf ("[int16_t]");
263 printf ("[int32_t]");
266 printf ("[int64_t]");
268 case FAT_SIZE_LEAST8_T:
269 printf ("[int_least8_t]");
271 case FAT_SIZE_LEAST16_T:
272 printf ("[int_least16_t]");
274 case FAT_SIZE_LEAST32_T:
275 printf ("[int_least32_t]");
277 case FAT_SIZE_LEAST64_T:
278 printf ("[int_least64_t]");
280 case FAT_SIZE_FAST8_T:
281 printf ("[int_fast8_t]");
283 case FAT_SIZE_FAST16_T:
284 printf ("[int_fast16_t]");
286 case FAT_SIZE_FAST32_T:
287 printf ("[int_fast32_t]");
289 case FAT_SIZE_FAST64_T:
290 printf ("[int_fast64_t]");
292 case FAT_SIZE_INTMAX_T:
293 printf ("[intmax_t]");
295 case FAT_SIZE_INTPTR_T:
296 printf ("[intptr_t]");
298 case FAT_SIZE_SIZE_T:
301 case FAT_SIZE_PTRDIFF_T:
302 printf ("[ptrdiff_t]");
307 switch (spec->unnumbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_MASK))
321 case FAT_OBJC_OBJECT:
327 case FAT_COUNT_POINTER:
343 size_t line_size = 0;
345 char *invalid_reason;
348 line_len = getline (&line, &line_size, stdin);
351 if (line_len > 0 && line[line_len - 1] == '\n')
352 line[--line_len] = '\0';
354 invalid_reason = NULL;
355 descr = format_c_parse (line, false, NULL, &invalid_reason);
357 format_print (descr);
360 printf ("%s\n", invalid_reason);
362 free (invalid_reason);
370 * For Emacs M-x compile
372 * 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"