2 Copyright (C) 2001-2004, 2006-2007, 2009, 2015 Free Software
4 Written by Bruno Haible <haible@clisp.cons.org>, 2002.
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"
30 #include "format-invalid.h"
33 #define _(str) gettext (str)
35 /* awk format strings are described in the gawk-3.1 documentation and
36 implemented in gawk-3.1.0/builtin.c: format_tree().
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 '*m$' or a nonempty digit sequence,
43 - is optionally followed by '.' and a precision specification: '*' (reads
44 an argument) or '*m$' or a nonempty digit sequence,
45 - is finished by a specifier
46 - '%', that needs no argument,
47 - 'c', that need a character argument,
48 - 's', that need a string argument,
49 - 'i', 'd', that need a signed integer argument,
50 - 'o', 'u', 'x', 'X', that need an unsigned integer argument,
51 - 'e', 'E', 'f', 'g', 'G', that need a floating-point argument.
52 Numbered ('%m$' or '*m$') and unnumbered argument specifications cannot
53 be used in the same string.
69 enum format_arg_type type;
74 unsigned int directives;
75 unsigned int numbered_arg_count;
76 unsigned int allocated;
77 struct numbered_arg *numbered;
80 /* Locale independent test for a decimal digit.
81 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
82 <ctype.h> isdigit must be an 'unsigned char'.) */
84 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
88 numbered_arg_compare (const void *p1, const void *p2)
90 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
91 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
93 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
97 format_parse (const char *format, bool translated, char *fdi,
98 char **invalid_reason)
100 const char *const format_start = format;
102 unsigned int unnumbered_arg_count;
106 spec.numbered_arg_count = 0;
108 spec.numbered = NULL;
109 unnumbered_arg_count = 0;
111 for (; *format != '\0';)
112 if (*format++ == '%')
115 unsigned int number = 0;
116 enum format_arg_type type;
118 FDI_SET (format - 1, FMTDIR_START);
121 if (isdigit (*format))
123 const char *f = format;
128 m = 10 * m + (*f - '0');
131 while (isdigit (*f));
137 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
138 FDI_SET (f, FMTDIR_ERROR);
147 while (*format == ' ' || *format == '+' || *format == '-'
148 || *format == '#' || *format == '0')
154 unsigned int width_number = 0;
158 if (isdigit (*format))
160 const char *f = format;
165 m = 10 * m + (*f - '0');
168 while (isdigit (*f));
175 INVALID_WIDTH_ARGNO_0 (spec.directives);
176 FDI_SET (f, FMTDIR_ERROR);
186 /* Numbered argument. */
188 /* Numbered and unnumbered specifications are exclusive. */
189 if (unnumbered_arg_count > 0)
191 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
192 FDI_SET (format - 1, FMTDIR_ERROR);
196 if (spec.allocated == spec.numbered_arg_count)
198 spec.allocated = 2 * spec.allocated + 1;
199 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
201 spec.numbered[spec.numbered_arg_count].number = width_number;
202 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
203 spec.numbered_arg_count++;
207 /* Unnumbered argument. */
209 /* Numbered and unnumbered specifications are exclusive. */
210 if (spec.numbered_arg_count > 0)
212 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
213 FDI_SET (format - 1, FMTDIR_ERROR);
217 if (spec.allocated == unnumbered_arg_count)
219 spec.allocated = 2 * spec.allocated + 1;
220 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
222 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
223 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
224 unnumbered_arg_count++;
227 else if (isdigit (*format))
229 do format++; while (isdigit (*format));
232 /* Parse precision. */
239 unsigned int precision_number = 0;
243 if (isdigit (*format))
245 const char *f = format;
250 m = 10 * m + (*f - '0');
253 while (isdigit (*f));
260 INVALID_PRECISION_ARGNO_0 (spec.directives);
261 FDI_SET (f, FMTDIR_ERROR);
264 precision_number = m;
269 if (precision_number)
271 /* Numbered argument. */
273 /* Numbered and unnumbered specifications are exclusive. */
274 if (unnumbered_arg_count > 0)
276 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
277 FDI_SET (format - 1, FMTDIR_ERROR);
281 if (spec.allocated == spec.numbered_arg_count)
283 spec.allocated = 2 * spec.allocated + 1;
284 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
286 spec.numbered[spec.numbered_arg_count].number = precision_number;
287 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
288 spec.numbered_arg_count++;
292 /* Unnumbered argument. */
294 /* Numbered and unnumbered specifications are exclusive. */
295 if (spec.numbered_arg_count > 0)
297 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
298 FDI_SET (format - 1, FMTDIR_ERROR);
302 if (spec.allocated == unnumbered_arg_count)
304 spec.allocated = 2 * spec.allocated + 1;
305 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
307 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
308 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
309 unnumbered_arg_count++;
312 else if (isdigit (*format))
314 do format++; while (isdigit (*format));
324 type = FAT_CHARACTER;
332 case 'u': case 'o': case 'x': case 'X':
333 type = FAT_UNSIGNED_INTEGER;
335 case 'e': case 'E': case 'f': case 'g': case 'G':
341 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
342 FDI_SET (format - 1, FMTDIR_ERROR);
347 INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
348 FDI_SET (format, FMTDIR_ERROR);
353 if (type != FAT_NONE)
357 /* Numbered argument. */
359 /* Numbered and unnumbered specifications are exclusive. */
360 if (unnumbered_arg_count > 0)
362 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
363 FDI_SET (format, FMTDIR_ERROR);
367 if (spec.allocated == spec.numbered_arg_count)
369 spec.allocated = 2 * spec.allocated + 1;
370 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
372 spec.numbered[spec.numbered_arg_count].number = number;
373 spec.numbered[spec.numbered_arg_count].type = type;
374 spec.numbered_arg_count++;
378 /* Unnumbered argument. */
380 /* Numbered and unnumbered specifications are exclusive. */
381 if (spec.numbered_arg_count > 0)
383 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
384 FDI_SET (format, FMTDIR_ERROR);
388 if (spec.allocated == unnumbered_arg_count)
390 spec.allocated = 2 * spec.allocated + 1;
391 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
393 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
394 spec.numbered[unnumbered_arg_count].type = type;
395 unnumbered_arg_count++;
399 FDI_SET (format, FMTDIR_END);
404 /* Convert the unnumbered argument array to numbered arguments. */
405 if (unnumbered_arg_count > 0)
406 spec.numbered_arg_count = unnumbered_arg_count;
407 /* Sort the numbered argument array, and eliminate duplicates. */
408 else if (spec.numbered_arg_count > 1)
413 qsort (spec.numbered, spec.numbered_arg_count,
414 sizeof (struct numbered_arg), numbered_arg_compare);
416 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
418 for (i = j = 0; i < spec.numbered_arg_count; i++)
419 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
421 enum format_arg_type type1 = spec.numbered[i].type;
422 enum format_arg_type type2 = spec.numbered[j-1].type;
423 enum format_arg_type type_both;
429 /* Incompatible types. */
430 type_both = FAT_NONE;
433 INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
437 spec.numbered[j-1].type = type_both;
443 spec.numbered[j].number = spec.numbered[i].number;
444 spec.numbered[j].type = spec.numbered[i].type;
448 spec.numbered_arg_count = j;
450 /* *invalid_reason has already been set above. */
454 result = XMALLOC (struct spec);
459 if (spec.numbered != NULL)
460 free (spec.numbered);
465 format_free (void *descr)
467 struct spec *spec = (struct spec *) descr;
469 if (spec->numbered != NULL)
470 free (spec->numbered);
475 format_get_number_of_directives (void *descr)
477 struct spec *spec = (struct spec *) descr;
479 return spec->directives;
483 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
484 formatstring_error_logger_t error_logger,
485 const char *pretty_msgid, const char *pretty_msgstr)
487 struct spec *spec1 = (struct spec *) msgid_descr;
488 struct spec *spec2 = (struct spec *) msgstr_descr;
491 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
494 unsigned int n1 = spec1->numbered_arg_count;
495 unsigned int n2 = spec2->numbered_arg_count;
497 /* Check the argument names are the same.
498 Both arrays are sorted. We search for the first difference. */
499 for (i = 0, j = 0; i < n1 || j < n2; )
501 int cmp = (i >= n1 ? 1 :
503 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
504 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
510 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
511 spec2->numbered[j].number, pretty_msgstr,
521 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
522 spec1->numbered[i].number, pretty_msgstr);
532 /* Check the argument types are the same. */
534 for (i = 0, j = 0; j < n2; )
536 if (spec1->numbered[i].number == spec2->numbered[j].number)
538 if (spec1->numbered[i].type != spec2->numbered[j].type)
541 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
542 pretty_msgid, pretty_msgstr,
543 spec2->numbered[j].number);
558 struct formatstring_parser formatstring_awk =
562 format_get_number_of_directives,
570 /* Test program: Print the argument list specification returned by
571 format_parse for strings read from standard input. */
576 format_print (void *descr)
578 struct spec *spec = (struct spec *) descr;
590 for (i = 0; i < spec->numbered_arg_count; i++)
592 unsigned int number = spec->numbered[i].number;
598 for (; last < number; last++)
600 switch (spec->numbered[i].type)
611 case FAT_UNSIGNED_INTEGER:
612 printf ("[unsigned]i");
631 size_t line_size = 0;
633 char *invalid_reason;
636 line_len = getline (&line, &line_size, stdin);
639 if (line_len > 0 && line[line_len - 1] == '\n')
640 line[--line_len] = '\0';
642 invalid_reason = NULL;
643 descr = format_parse (line, false, NULL, &invalid_reason);
645 format_print (descr);
648 printf ("%s\n", invalid_reason);
650 free (invalid_reason);
658 * For Emacs M-x compile
660 * 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"