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 /* awk format strings are described in the gawk-3.1 documentation and
35 implemented in gawk-3.1.0/builtin.c: format_tree().
37 - starts with '%' or '%m$' where m is a positive integer,
38 - is optionally followed by any of the characters '#', '0', '-', ' ', '+',
39 each of which acts as a flag,
40 - is optionally followed by a width specification: '*' (reads an argument)
41 or '*m$' or a nonempty digit sequence,
42 - is optionally followed by '.' and a precision specification: '*' (reads
43 an argument) or '*m$' or a nonempty digit sequence,
44 - is finished by a specifier
45 - '%', that needs no argument,
46 - 'c', that need a character argument,
47 - 's', that need a string argument,
48 - 'i', 'd', that need a signed integer argument,
49 - 'o', 'u', 'x', 'X', that need an unsigned integer argument,
50 - 'e', 'E', 'f', 'g', 'G', that need a floating-point argument.
51 Numbered ('%m$' or '*m$') and unnumbered argument specifications cannot
52 be used in the same string.
68 enum format_arg_type type;
73 unsigned int directives;
74 unsigned int numbered_arg_count;
75 unsigned int allocated;
76 struct numbered_arg *numbered;
79 /* Locale independent test for a decimal digit.
80 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
81 <ctype.h> isdigit must be an 'unsigned char'.) */
83 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
87 numbered_arg_compare (const void *p1, const void *p2)
89 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
90 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
92 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
96 format_parse (const char *format, bool translated, char *fdi,
97 char **invalid_reason)
99 const char *const format_start = format;
101 unsigned int unnumbered_arg_count;
105 spec.numbered_arg_count = 0;
107 spec.numbered = NULL;
108 unnumbered_arg_count = 0;
110 for (; *format != '\0';)
111 if (*format++ == '%')
114 unsigned int number = 0;
115 enum format_arg_type type;
117 FDI_SET (format - 1, FMTDIR_START);
120 if (isdigit (*format))
122 const char *f = format;
127 m = 10 * m + (*f - '0');
130 while (isdigit (*f));
136 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
137 FDI_SET (f, FMTDIR_ERROR);
146 while (*format == ' ' || *format == '+' || *format == '-'
147 || *format == '#' || *format == '0')
153 unsigned int width_number = 0;
157 if (isdigit (*format))
159 const char *f = format;
164 m = 10 * m + (*f - '0');
167 while (isdigit (*f));
174 INVALID_WIDTH_ARGNO_0 (spec.directives);
175 FDI_SET (f, FMTDIR_ERROR);
185 /* Numbered argument. */
187 /* Numbered and unnumbered specifications are exclusive. */
188 if (unnumbered_arg_count > 0)
190 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
191 FDI_SET (format - 1, FMTDIR_ERROR);
195 if (spec.allocated == spec.numbered_arg_count)
197 spec.allocated = 2 * spec.allocated + 1;
198 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
200 spec.numbered[spec.numbered_arg_count].number = width_number;
201 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
202 spec.numbered_arg_count++;
206 /* Unnumbered argument. */
208 /* Numbered and unnumbered specifications are exclusive. */
209 if (spec.numbered_arg_count > 0)
211 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
212 FDI_SET (format - 1, FMTDIR_ERROR);
216 if (spec.allocated == unnumbered_arg_count)
218 spec.allocated = 2 * spec.allocated + 1;
219 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
221 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
222 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
223 unnumbered_arg_count++;
226 else if (isdigit (*format))
228 do format++; while (isdigit (*format));
231 /* Parse precision. */
238 unsigned int precision_number = 0;
242 if (isdigit (*format))
244 const char *f = format;
249 m = 10 * m + (*f - '0');
252 while (isdigit (*f));
259 INVALID_PRECISION_ARGNO_0 (spec.directives);
260 FDI_SET (f, FMTDIR_ERROR);
263 precision_number = m;
268 if (precision_number)
270 /* Numbered argument. */
272 /* Numbered and unnumbered specifications are exclusive. */
273 if (unnumbered_arg_count > 0)
275 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
276 FDI_SET (format - 1, FMTDIR_ERROR);
280 if (spec.allocated == spec.numbered_arg_count)
282 spec.allocated = 2 * spec.allocated + 1;
283 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
285 spec.numbered[spec.numbered_arg_count].number = precision_number;
286 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
287 spec.numbered_arg_count++;
291 /* Unnumbered argument. */
293 /* Numbered and unnumbered specifications are exclusive. */
294 if (spec.numbered_arg_count > 0)
296 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
297 FDI_SET (format - 1, FMTDIR_ERROR);
301 if (spec.allocated == unnumbered_arg_count)
303 spec.allocated = 2 * spec.allocated + 1;
304 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
306 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
307 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
308 unnumbered_arg_count++;
311 else if (isdigit (*format))
313 do format++; while (isdigit (*format));
323 type = FAT_CHARACTER;
331 case 'u': case 'o': case 'x': case 'X':
332 type = FAT_UNSIGNED_INTEGER;
334 case 'e': case 'E': case 'f': case 'g': case 'G':
340 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
341 FDI_SET (format - 1, FMTDIR_ERROR);
346 INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
347 FDI_SET (format, FMTDIR_ERROR);
352 if (type != FAT_NONE)
356 /* Numbered argument. */
358 /* Numbered and unnumbered specifications are exclusive. */
359 if (unnumbered_arg_count > 0)
361 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
362 FDI_SET (format, FMTDIR_ERROR);
366 if (spec.allocated == spec.numbered_arg_count)
368 spec.allocated = 2 * spec.allocated + 1;
369 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
371 spec.numbered[spec.numbered_arg_count].number = number;
372 spec.numbered[spec.numbered_arg_count].type = type;
373 spec.numbered_arg_count++;
377 /* Unnumbered argument. */
379 /* Numbered and unnumbered specifications are exclusive. */
380 if (spec.numbered_arg_count > 0)
382 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
383 FDI_SET (format, FMTDIR_ERROR);
387 if (spec.allocated == unnumbered_arg_count)
389 spec.allocated = 2 * spec.allocated + 1;
390 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
392 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
393 spec.numbered[unnumbered_arg_count].type = type;
394 unnumbered_arg_count++;
398 FDI_SET (format, FMTDIR_END);
403 /* Convert the unnumbered argument array to numbered arguments. */
404 if (unnumbered_arg_count > 0)
405 spec.numbered_arg_count = unnumbered_arg_count;
406 /* Sort the numbered argument array, and eliminate duplicates. */
407 else if (spec.numbered_arg_count > 1)
412 qsort (spec.numbered, spec.numbered_arg_count,
413 sizeof (struct numbered_arg), numbered_arg_compare);
415 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
417 for (i = j = 0; i < spec.numbered_arg_count; i++)
418 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
420 enum format_arg_type type1 = spec.numbered[i].type;
421 enum format_arg_type type2 = spec.numbered[j-1].type;
422 enum format_arg_type type_both;
428 /* Incompatible types. */
429 type_both = FAT_NONE;
432 INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
436 spec.numbered[j-1].type = type_both;
442 spec.numbered[j].number = spec.numbered[i].number;
443 spec.numbered[j].type = spec.numbered[i].type;
447 spec.numbered_arg_count = j;
449 /* *invalid_reason has already been set above. */
453 result = XMALLOC (struct spec);
458 if (spec.numbered != NULL)
459 free (spec.numbered);
464 format_free (void *descr)
466 struct spec *spec = (struct spec *) descr;
468 if (spec->numbered != NULL)
469 free (spec->numbered);
474 format_get_number_of_directives (void *descr)
476 struct spec *spec = (struct spec *) descr;
478 return spec->directives;
482 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
483 formatstring_error_logger_t error_logger,
484 const char *pretty_msgid, const char *pretty_msgstr)
486 struct spec *spec1 = (struct spec *) msgid_descr;
487 struct spec *spec2 = (struct spec *) msgstr_descr;
490 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
493 unsigned int n1 = spec1->numbered_arg_count;
494 unsigned int n2 = spec2->numbered_arg_count;
496 /* Check the argument names are the same.
497 Both arrays are sorted. We search for the first difference. */
498 for (i = 0, j = 0; i < n1 || j < n2; )
500 int cmp = (i >= n1 ? 1 :
502 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
503 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
509 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
510 spec2->numbered[j].number, pretty_msgstr,
520 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
521 spec1->numbered[i].number, pretty_msgstr);
531 /* Check the argument types are the same. */
533 for (i = 0, j = 0; j < n2; )
535 if (spec1->numbered[i].number == spec2->numbered[j].number)
537 if (spec1->numbered[i].type != spec2->numbered[j].type)
540 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
541 pretty_msgid, pretty_msgstr,
542 spec2->numbered[j].number);
557 struct formatstring_parser formatstring_awk =
561 format_get_number_of_directives,
569 /* Test program: Print the argument list specification returned by
570 format_parse for strings read from standard input. */
575 format_print (void *descr)
577 struct spec *spec = (struct spec *) descr;
589 for (i = 0; i < spec->numbered_arg_count; i++)
591 unsigned int number = spec->numbered[i].number;
597 for (; last < number; last++)
599 switch (spec->numbered[i].type)
610 case FAT_UNSIGNED_INTEGER:
611 printf ("[unsigned]i");
630 size_t line_size = 0;
632 char *invalid_reason;
635 line_len = getline (&line, &line_size, stdin);
638 if (line_len > 0 && line[line_len - 1] == '\n')
639 line[--line_len] = '\0';
641 invalid_reason = NULL;
642 descr = format_parse (line, false, NULL, &invalid_reason);
644 format_print (descr);
647 printf ("%s\n", invalid_reason);
649 free (invalid_reason);
657 * For Emacs M-x compile
659 * 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-awk.c ../gnulib-lib/libgettextlib.la"