2 Copyright (C) 2001-2004, 2006-2007, 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2002.
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"
29 #include "format-invalid.h"
32 #define _(str) gettext (str)
34 /* Tcl format strings are described in the tcl8.3.3/doc/format.n manual
35 page and implemented in the function Tcl_FormatObjCmd in
36 tcl8.3.3/generic/tclCmdAH.c.
38 - starts with '%' or '%m$' where m is a positive integer,
39 - is optionally followed by any of the characters '#', '0', '-', ' ', '+',
40 each of which acts as a flag,
41 - is optionally followed by a width specification: '*' (reads an argument)
42 or a nonempty digit sequence,
43 - is optionally followed by '.' and a precision specification: '*' (reads
44 an argument) or a nonempty digit sequence,
45 - is optionally followed by a size specifier, 'h' or 'l'. 'l' is ignored.
46 - is finished by a specifier
47 - '%', that needs no argument,
48 - 'c', that needs a character argument,
49 - 's', that needs a string argument,
50 - 'i', 'd', that need a signed integer argument,
51 - 'o', 'u', 'x', 'X', that need an unsigned integer argument,
52 - 'e', 'E', 'f', 'g', 'G', that need a floating-point argument.
53 Numbered ('%m$') and unnumbered argument specifications cannot be used
65 FAT_SHORT_UNSIGNED_INTEGER,
72 enum format_arg_type type;
77 unsigned int directives;
78 unsigned int numbered_arg_count;
79 unsigned int allocated;
80 struct numbered_arg *numbered;
83 /* Locale independent test for a decimal digit.
84 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
85 <ctype.h> isdigit must be an 'unsigned char'.) */
87 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
91 numbered_arg_compare (const void *p1, const void *p2)
93 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
94 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
96 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
100 format_parse (const char *format, bool translated, char *fdi,
101 char **invalid_reason)
103 const char *const format_start = format;
106 bool seen_numbered_arg;
107 bool seen_unnumbered_arg;
111 spec.numbered_arg_count = 0;
113 spec.numbered = NULL;
114 seen_numbered_arg = false;
115 seen_unnumbered_arg = false;
118 for (; *format != '\0';)
119 if (*format++ == '%')
122 FDI_SET (format - 1, FMTDIR_START);
127 bool is_numbered_arg;
129 enum format_arg_type type;
131 is_numbered_arg = false;
132 if (isdigit (*format))
134 const char *f = format;
139 m = 10 * m + (*f - '0');
142 while (isdigit (*f));
148 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
149 FDI_SET (f, FMTDIR_ERROR);
155 /* Numbered and unnumbered specifications are exclusive. */
156 if (seen_unnumbered_arg)
158 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
159 FDI_SET (format - 1, FMTDIR_ERROR);
162 is_numbered_arg = true;
163 seen_numbered_arg = true;
167 /* Numbered and unnumbered specifications are exclusive. */
168 if (!is_numbered_arg)
170 if (seen_numbered_arg)
172 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
173 FDI_SET (format - 1, FMTDIR_ERROR);
176 seen_unnumbered_arg = true;
180 while (*format == ' ' || *format == '+' || *format == '-'
181 || *format == '#' || *format == '0')
189 if (spec.allocated == spec.numbered_arg_count)
191 spec.allocated = 2 * spec.allocated + 1;
192 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
194 spec.numbered[spec.numbered_arg_count].number = number;
195 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
196 spec.numbered_arg_count++;
200 else if (isdigit (*format))
202 do format++; while (isdigit (*format));
205 /* Parse precision. */
214 if (spec.allocated == spec.numbered_arg_count)
216 spec.allocated = 2 * spec.allocated + 1;
217 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
219 spec.numbered[spec.numbered_arg_count].number = number;
220 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
221 spec.numbered_arg_count++;
225 else if (isdigit (*format))
227 do format++; while (isdigit (*format));
231 /* Parse optional size specification. */
234 short_flag = true, format++;
235 else if (*format == 'l')
241 type = FAT_CHARACTER;
247 type = (short_flag ? FAT_SHORT_INTEGER : FAT_INTEGER);
249 case 'u': case 'o': case 'x': case 'X':
250 type = (short_flag ? FAT_SHORT_UNSIGNED_INTEGER : FAT_UNSIGNED_INTEGER);
252 case 'e': case 'E': case 'f': case 'g': case 'G':
258 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
259 FDI_SET (format - 1, FMTDIR_ERROR);
264 INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
265 FDI_SET (format, FMTDIR_ERROR);
270 if (spec.allocated == spec.numbered_arg_count)
272 spec.allocated = 2 * spec.allocated + 1;
273 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
275 spec.numbered[spec.numbered_arg_count].number = number;
276 spec.numbered[spec.numbered_arg_count].type = type;
277 spec.numbered_arg_count++;
282 FDI_SET (format, FMTDIR_END);
287 /* Sort the numbered argument array, and eliminate duplicates. */
288 if (spec.numbered_arg_count > 1)
293 qsort (spec.numbered, spec.numbered_arg_count,
294 sizeof (struct numbered_arg), numbered_arg_compare);
296 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
298 for (i = j = 0; i < spec.numbered_arg_count; i++)
299 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
301 enum format_arg_type type1 = spec.numbered[i].type;
302 enum format_arg_type type2 = spec.numbered[j-1].type;
303 enum format_arg_type type_both;
309 /* Incompatible types. */
310 type_both = FAT_NONE;
313 INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
317 spec.numbered[j-1].type = type_both;
323 spec.numbered[j].number = spec.numbered[i].number;
324 spec.numbered[j].type = spec.numbered[i].type;
328 spec.numbered_arg_count = j;
330 /* *invalid_reason has already been set above. */
334 result = XMALLOC (struct spec);
339 if (spec.numbered != NULL)
340 free (spec.numbered);
345 format_free (void *descr)
347 struct spec *spec = (struct spec *) descr;
349 if (spec->numbered != NULL)
350 free (spec->numbered);
355 format_get_number_of_directives (void *descr)
357 struct spec *spec = (struct spec *) descr;
359 return spec->directives;
363 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
364 formatstring_error_logger_t error_logger,
365 const char *pretty_msgid, const char *pretty_msgstr)
367 struct spec *spec1 = (struct spec *) msgid_descr;
368 struct spec *spec2 = (struct spec *) msgstr_descr;
371 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
374 unsigned int n1 = spec1->numbered_arg_count;
375 unsigned int n2 = spec2->numbered_arg_count;
377 /* Check the argument names are the same.
378 Both arrays are sorted. We search for the first difference. */
379 for (i = 0, j = 0; i < n1 || j < n2; )
381 int cmp = (i >= n1 ? 1 :
383 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
384 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
390 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
391 spec2->numbered[j].number, pretty_msgstr,
401 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
402 spec1->numbered[i].number, pretty_msgstr);
412 /* Check the argument types are the same. */
414 for (i = 0, j = 0; j < n2; )
416 if (spec1->numbered[i].number == spec2->numbered[j].number)
418 if (spec1->numbered[i].type != spec2->numbered[j].type)
421 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
422 pretty_msgid, pretty_msgstr,
423 spec2->numbered[j].number);
438 struct formatstring_parser formatstring_tcl =
442 format_get_number_of_directives,
450 /* Test program: Print the argument list specification returned by
451 format_parse for strings read from standard input. */
456 format_print (void *descr)
458 struct spec *spec = (struct spec *) descr;
470 for (i = 0; i < spec->numbered_arg_count; i++)
472 unsigned int number = spec->numbered[i].number;
478 for (; last < number; last++)
480 switch (spec->numbered[i].type)
491 case FAT_UNSIGNED_INTEGER:
492 printf ("[unsigned]i");
494 case FAT_SHORT_INTEGER:
497 case FAT_SHORT_UNSIGNED_INTEGER:
498 printf ("[unsigned]hi");
517 size_t line_size = 0;
519 char *invalid_reason;
522 line_len = getline (&line, &line_size, stdin);
525 if (line_len > 0 && line[line_len - 1] == '\n')
526 line[--line_len] = '\0';
528 invalid_reason = NULL;
529 descr = format_parse (line, false, NULL, &invalid_reason);
531 format_print (descr);
534 printf ("%s\n", invalid_reason);
536 free (invalid_reason);
544 * For Emacs M-x compile
546 * 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-tcl.c ../gnulib-lib/libgettextlib.la"