Add -lm dependency for gettextlib to fix LTO build
[platform/upstream/gettext.git] / gettext-tools / src / format-awk.c
1 /* awk format strings.
2    Copyright (C) 2001-2004, 2006-2007, 2009, 2015 Free Software
3    Foundation, Inc.
4    Written by Bruno Haible <haible@clisp.cons.org>, 2002.
5
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.
10
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.
15
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/>.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #include "format.h"
27 #include "c-ctype.h"
28 #include "xalloc.h"
29 #include "xvasprintf.h"
30 #include "format-invalid.h"
31 #include "gettext.h"
32
33 #define _(str) gettext (str)
34
35 /* awk format strings are described in the gawk-3.1 documentation and
36    implemented in gawk-3.1.0/builtin.c: format_tree().
37    A directive
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.
54  */
55
56 enum format_arg_type
57 {
58   FAT_NONE,
59   FAT_CHARACTER,
60   FAT_STRING,
61   FAT_INTEGER,
62   FAT_UNSIGNED_INTEGER,
63   FAT_FLOAT
64 };
65
66 struct numbered_arg
67 {
68   unsigned int number;
69   enum format_arg_type type;
70 };
71
72 struct spec
73 {
74   unsigned int directives;
75   unsigned int numbered_arg_count;
76   unsigned int allocated;
77   struct numbered_arg *numbered;
78 };
79
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'.)  */
83 #undef isdigit
84 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
85
86
87 static int
88 numbered_arg_compare (const void *p1, const void *p2)
89 {
90   unsigned int n1 = ((const struct numbered_arg *) p1)->number;
91   unsigned int n2 = ((const struct numbered_arg *) p2)->number;
92
93   return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
94 }
95
96 static void *
97 format_parse (const char *format, bool translated, char *fdi,
98               char **invalid_reason)
99 {
100   const char *const format_start = format;
101   struct spec spec;
102   unsigned int unnumbered_arg_count;
103   struct spec *result;
104
105   spec.directives = 0;
106   spec.numbered_arg_count = 0;
107   spec.allocated = 0;
108   spec.numbered = NULL;
109   unnumbered_arg_count = 0;
110
111   for (; *format != '\0';)
112     if (*format++ == '%')
113       {
114         /* A directive.  */
115         unsigned int number = 0;
116         enum format_arg_type type;
117
118         FDI_SET (format - 1, FMTDIR_START);
119         spec.directives++;
120
121         if (isdigit (*format))
122           {
123             const char *f = format;
124             unsigned int m = 0;
125
126             do
127               {
128                 m = 10 * m + (*f - '0');
129                 f++;
130               }
131             while (isdigit (*f));
132
133             if (*f == '$')
134               {
135                 if (m == 0)
136                   {
137                     *invalid_reason = INVALID_ARGNO_0 (spec.directives);
138                     FDI_SET (f, FMTDIR_ERROR);
139                     goto bad_format;
140                   }
141                 number = m;
142                 format = ++f;
143               }
144           }
145
146         /* Parse flags.  */
147         while (*format == ' ' || *format == '+' || *format == '-'
148                || *format == '#' || *format == '0')
149           format++;
150
151         /* Parse width.  */
152         if (*format == '*')
153           {
154             unsigned int width_number = 0;
155
156             format++;
157
158             if (isdigit (*format))
159               {
160                 const char *f = format;
161                 unsigned int m = 0;
162
163                 do
164                   {
165                     m = 10 * m + (*f - '0');
166                     f++;
167                   }
168                 while (isdigit (*f));
169
170                 if (*f == '$')
171                   {
172                     if (m == 0)
173                       {
174                         *invalid_reason =
175                           INVALID_WIDTH_ARGNO_0 (spec.directives);
176                         FDI_SET (f, FMTDIR_ERROR);
177                         goto bad_format;
178                       }
179                     width_number = m;
180                     format = ++f;
181                   }
182               }
183
184             if (width_number)
185               {
186                 /* Numbered argument.  */
187
188                 /* Numbered and unnumbered specifications are exclusive.  */
189                 if (unnumbered_arg_count > 0)
190                   {
191                     *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
192                     FDI_SET (format - 1, FMTDIR_ERROR);
193                     goto bad_format;
194                   }
195
196                 if (spec.allocated == spec.numbered_arg_count)
197                   {
198                     spec.allocated = 2 * spec.allocated + 1;
199                     spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
200                   }
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++;
204               }
205             else
206               {
207                 /* Unnumbered argument.  */
208
209                 /* Numbered and unnumbered specifications are exclusive.  */
210                 if (spec.numbered_arg_count > 0)
211                   {
212                     *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
213                     FDI_SET (format - 1, FMTDIR_ERROR);
214                     goto bad_format;
215                   }
216
217                 if (spec.allocated == unnumbered_arg_count)
218                   {
219                     spec.allocated = 2 * spec.allocated + 1;
220                     spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
221                   }
222                 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
223                 spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
224                 unnumbered_arg_count++;
225               }
226           }
227         else if (isdigit (*format))
228           {
229             do format++; while (isdigit (*format));
230           }
231
232         /* Parse precision.  */
233         if (*format == '.')
234           {
235             format++;
236
237             if (*format == '*')
238               {
239                 unsigned int precision_number = 0;
240
241                 format++;
242
243                 if (isdigit (*format))
244                   {
245                     const char *f = format;
246                     unsigned int m = 0;
247
248                     do
249                       {
250                         m = 10 * m + (*f - '0');
251                         f++;
252                       }
253                     while (isdigit (*f));
254
255                     if (*f == '$')
256                       {
257                         if (m == 0)
258                           {
259                             *invalid_reason =
260                               INVALID_PRECISION_ARGNO_0 (spec.directives);
261                             FDI_SET (f, FMTDIR_ERROR);
262                             goto bad_format;
263                           }
264                         precision_number = m;
265                         format = ++f;
266                       }
267                   }
268
269                 if (precision_number)
270                   {
271                     /* Numbered argument.  */
272
273                     /* Numbered and unnumbered specifications are exclusive.  */
274                     if (unnumbered_arg_count > 0)
275                       {
276                         *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
277                         FDI_SET (format - 1, FMTDIR_ERROR);
278                         goto bad_format;
279                       }
280
281                     if (spec.allocated == spec.numbered_arg_count)
282                       {
283                         spec.allocated = 2 * spec.allocated + 1;
284                         spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
285                       }
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++;
289                   }
290                 else
291                   {
292                     /* Unnumbered argument.  */
293
294                     /* Numbered and unnumbered specifications are exclusive.  */
295                     if (spec.numbered_arg_count > 0)
296                       {
297                         *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
298                         FDI_SET (format - 1, FMTDIR_ERROR);
299                         goto bad_format;
300                       }
301
302                     if (spec.allocated == unnumbered_arg_count)
303                       {
304                         spec.allocated = 2 * spec.allocated + 1;
305                         spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
306                       }
307                     spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
308                     spec.numbered[unnumbered_arg_count].type = FAT_INTEGER;
309                     unnumbered_arg_count++;
310                   }
311               }
312             else if (isdigit (*format))
313               {
314                 do format++; while (isdigit (*format));
315               }
316           }
317
318         switch (*format)
319           {
320           case '%':
321             type = FAT_NONE;
322             break;
323           case 'c':
324             type = FAT_CHARACTER;
325             break;
326           case 's':
327             type = FAT_STRING;
328             break;
329           case 'i': case 'd':
330             type = FAT_INTEGER;
331             break;
332           case 'u': case 'o': case 'x': case 'X':
333             type = FAT_UNSIGNED_INTEGER;
334             break;
335           case 'e': case 'E': case 'f': case 'g': case 'G':
336             type = FAT_FLOAT;
337             break;
338           default:
339             if (*format == '\0')
340               {
341                 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
342                 FDI_SET (format - 1, FMTDIR_ERROR);
343               }
344             else
345               {
346                 *invalid_reason =
347                   INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
348                 FDI_SET (format, FMTDIR_ERROR);
349               }
350             goto bad_format;
351           }
352
353         if (type != FAT_NONE)
354           {
355             if (number)
356               {
357                 /* Numbered argument.  */
358
359                 /* Numbered and unnumbered specifications are exclusive.  */
360                 if (unnumbered_arg_count > 0)
361                   {
362                     *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
363                     FDI_SET (format, FMTDIR_ERROR);
364                     goto bad_format;
365                   }
366
367                 if (spec.allocated == spec.numbered_arg_count)
368                   {
369                     spec.allocated = 2 * spec.allocated + 1;
370                     spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
371                   }
372                 spec.numbered[spec.numbered_arg_count].number = number;
373                 spec.numbered[spec.numbered_arg_count].type = type;
374                 spec.numbered_arg_count++;
375               }
376             else
377               {
378                 /* Unnumbered argument.  */
379
380                 /* Numbered and unnumbered specifications are exclusive.  */
381                 if (spec.numbered_arg_count > 0)
382                   {
383                     *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
384                     FDI_SET (format, FMTDIR_ERROR);
385                     goto bad_format;
386                   }
387
388                 if (spec.allocated == unnumbered_arg_count)
389                   {
390                     spec.allocated = 2 * spec.allocated + 1;
391                     spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
392                   }
393                 spec.numbered[unnumbered_arg_count].number = unnumbered_arg_count + 1;
394                 spec.numbered[unnumbered_arg_count].type = type;
395                 unnumbered_arg_count++;
396               }
397           }
398
399         FDI_SET (format, FMTDIR_END);
400
401         format++;
402       }
403
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)
409     {
410       unsigned int i, j;
411       bool err;
412
413       qsort (spec.numbered, spec.numbered_arg_count,
414              sizeof (struct numbered_arg), numbered_arg_compare);
415
416       /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i.  */
417       err = false;
418       for (i = j = 0; i < spec.numbered_arg_count; i++)
419         if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
420           {
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;
424
425             if (type1 == type2)
426               type_both = type1;
427             else
428               {
429                 /* Incompatible types.  */
430                 type_both = FAT_NONE;
431                 if (!err)
432                   *invalid_reason =
433                     INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
434                 err = true;
435               }
436
437             spec.numbered[j-1].type = type_both;
438           }
439         else
440           {
441             if (j < i)
442               {
443                 spec.numbered[j].number = spec.numbered[i].number;
444                 spec.numbered[j].type = spec.numbered[i].type;
445               }
446             j++;
447           }
448       spec.numbered_arg_count = j;
449       if (err)
450         /* *invalid_reason has already been set above.  */
451         goto bad_format;
452     }
453
454   result = XMALLOC (struct spec);
455   *result = spec;
456   return result;
457
458  bad_format:
459   if (spec.numbered != NULL)
460     free (spec.numbered);
461   return NULL;
462 }
463
464 static void
465 format_free (void *descr)
466 {
467   struct spec *spec = (struct spec *) descr;
468
469   if (spec->numbered != NULL)
470     free (spec->numbered);
471   free (spec);
472 }
473
474 static int
475 format_get_number_of_directives (void *descr)
476 {
477   struct spec *spec = (struct spec *) descr;
478
479   return spec->directives;
480 }
481
482 static bool
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)
486 {
487   struct spec *spec1 = (struct spec *) msgid_descr;
488   struct spec *spec2 = (struct spec *) msgstr_descr;
489   bool err = false;
490
491   if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
492     {
493       unsigned int i, j;
494       unsigned int n1 = spec1->numbered_arg_count;
495       unsigned int n2 = spec2->numbered_arg_count;
496
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; )
500         {
501           int cmp = (i >= n1 ? 1 :
502                      j >= n2 ? -1 :
503                      spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
504                      spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
505                      0);
506
507           if (cmp > 0)
508             {
509               if (error_logger)
510                 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
511                               spec2->numbered[j].number, pretty_msgstr,
512                               pretty_msgid);
513               err = true;
514               break;
515             }
516           else if (cmp < 0)
517             {
518               if (equality)
519                 {
520                   if (error_logger)
521                     error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
522                                   spec1->numbered[i].number, pretty_msgstr);
523                   err = true;
524                   break;
525                 }
526               else
527                 i++;
528             }
529           else
530             j++, i++;
531         }
532       /* Check the argument types are the same.  */
533       if (!err)
534         for (i = 0, j = 0; j < n2; )
535           {
536             if (spec1->numbered[i].number == spec2->numbered[j].number)
537               {
538                 if (spec1->numbered[i].type != spec2->numbered[j].type)
539                   {
540                     if (error_logger)
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);
544                     err = true;
545                     break;
546                   }
547                 j++, i++;
548               }
549             else
550               i++;
551           }
552     }
553
554   return err;
555 }
556
557
558 struct formatstring_parser formatstring_awk =
559 {
560   format_parse,
561   format_free,
562   format_get_number_of_directives,
563   NULL,
564   format_check
565 };
566
567
568 #ifdef TEST
569
570 /* Test program: Print the argument list specification returned by
571    format_parse for strings read from standard input.  */
572
573 #include <stdio.h>
574
575 static void
576 format_print (void *descr)
577 {
578   struct spec *spec = (struct spec *) descr;
579   unsigned int last;
580   unsigned int i;
581
582   if (spec == NULL)
583     {
584       printf ("INVALID");
585       return;
586     }
587
588   printf ("(");
589   last = 1;
590   for (i = 0; i < spec->numbered_arg_count; i++)
591     {
592       unsigned int number = spec->numbered[i].number;
593
594       if (i > 0)
595         printf (" ");
596       if (number < last)
597         abort ();
598       for (; last < number; last++)
599         printf ("_ ");
600       switch (spec->numbered[i].type)
601         {
602         case FAT_CHARACTER:
603           printf ("c");
604           break;
605         case FAT_STRING:
606           printf ("s");
607           break;
608         case FAT_INTEGER:
609           printf ("i");
610           break;
611         case FAT_UNSIGNED_INTEGER:
612           printf ("[unsigned]i");
613           break;
614         case FAT_FLOAT:
615           printf ("f");
616           break;
617         default:
618           abort ();
619         }
620       last = number + 1;
621     }
622   printf (")");
623 }
624
625 int
626 main ()
627 {
628   for (;;)
629     {
630       char *line = NULL;
631       size_t line_size = 0;
632       int line_len;
633       char *invalid_reason;
634       void *descr;
635
636       line_len = getline (&line, &line_size, stdin);
637       if (line_len < 0)
638         break;
639       if (line_len > 0 && line[line_len - 1] == '\n')
640         line[--line_len] = '\0';
641
642       invalid_reason = NULL;
643       descr = format_parse (line, false, NULL, &invalid_reason);
644
645       format_print (descr);
646       printf ("\n");
647       if (descr == NULL)
648         printf ("%s\n", invalid_reason);
649
650       free (invalid_reason);
651       free (line);
652     }
653
654   return 0;
655 }
656
657 /*
658  * For Emacs M-x compile
659  * Local Variables:
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"
661  * End:
662  */
663
664 #endif /* TEST */