Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / src / format.c
1 /* Format strings.
2    Copyright (C) 2001-2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4
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.
9
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.
14
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/>.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 /* Specification.  */
23 #include "format.h"
24
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "message.h"
30 #include "gettext.h"
31
32 #define _(str) gettext (str)
33
34 /* Table of all format string parsers.  */
35 struct formatstring_parser *formatstring_parsers[NFORMATS] =
36 {
37   /* format_c */                &formatstring_c,
38   /* format_objc */             &formatstring_objc,
39   /* format_sh */               &formatstring_sh,
40   /* format_python */           &formatstring_python,
41   /* format_lisp */             &formatstring_lisp,
42   /* format_elisp */            &formatstring_elisp,
43   /* format_librep */           &formatstring_librep,
44   /* format_scheme */           &formatstring_scheme,
45   /* format_smalltalk */        &formatstring_smalltalk,
46   /* format_java */             &formatstring_java,
47   /* format_csharp */           &formatstring_csharp,
48   /* format_awk */              &formatstring_awk,
49   /* format_pascal */           &formatstring_pascal,
50   /* format_ycp */              &formatstring_ycp,
51   /* format_tcl */              &formatstring_tcl,
52   /* format_perl */             &formatstring_perl,
53   /* format_perl_brace */       &formatstring_perl_brace,
54   /* format_php */              &formatstring_php,
55   /* format_gcc_internal */     &formatstring_gcc_internal,
56   /* format_gfc_internal */     &formatstring_gfc_internal,
57   /* format_qt */               &formatstring_qt,
58   /* format_qt_plural */        &formatstring_qt_plural,
59   /* format_kde */              &formatstring_kde,
60   /* format_boost */            &formatstring_boost
61 };
62
63 /* Check whether both formats strings contain compatible format
64    specifications for format type i (0 <= i < NFORMATS).  */
65 int
66 check_msgid_msgstr_format_i (const char *msgid, const char *msgid_plural,
67                              const char *msgstr, size_t msgstr_len,
68                              size_t i,
69                              struct argument_range range,
70                              const struct plural_distribution *distribution,
71                              formatstring_error_logger_t error_logger)
72 {
73   int seen_errors = 0;
74
75   /* At runtime, we can assume the program passes arguments that fit well for
76      msgid.  We must signal an error if msgstr wants more arguments that msgid
77      accepts.
78      If msgstr wants fewer arguments than msgid, it wouldn't lead to a crash
79      at runtime, but we nevertheless give an error because
80      1) this situation occurs typically after the programmer has added some
81         arguments to msgid, so we must make the translator specially aware
82         of it (more than just "fuzzy"),
83      2) it is generally wrong if a translation wants to ignore arguments that
84         are used by other translations.  */
85
86   struct formatstring_parser *parser = formatstring_parsers[i];
87   char *invalid_reason = NULL;
88   void *msgid_descr =
89     parser->parse (msgid_plural != NULL ? msgid_plural : msgid, false, NULL,
90                    &invalid_reason);
91
92   if (msgid_descr != NULL)
93     {
94       const char *pretty_msgid =
95         (msgid_plural != NULL ? "msgid_plural" : "msgid");
96       char buf[18+1];
97       const char *pretty_msgstr = "msgstr";
98       bool has_plural_translations = (strlen (msgstr) + 1 < msgstr_len);
99       const char *p_end = msgstr + msgstr_len;
100       const char *p;
101       unsigned int j;
102
103       for (p = msgstr, j = 0; p < p_end; p += strlen (p) + 1, j++)
104         {
105           void *msgstr_descr;
106
107           if (msgid_plural != NULL)
108             {
109               sprintf (buf, "msgstr[%u]", j);
110               pretty_msgstr = buf;
111             }
112
113           msgstr_descr = parser->parse (p, true, NULL, &invalid_reason);
114
115           if (msgstr_descr != NULL)
116             {
117               /* Use strict checking (require same number of format
118                  directives on both sides) if the message has no plurals,
119                  or if msgid_plural exists but on the msgstr[] side
120                  there is only msgstr[0], or if distribution->often[j]
121                  indicates that the variant applies to infinitely many
122                  values of N and the N range is not restricted in a way
123                  that the variant applies to only one N.
124                  Use relaxed checking when there are at least two
125                  msgstr[] forms and the distribution does not give more
126                  precise information.  */
127               bool strict_checking =
128                 (msgid_plural == NULL
129                  || !has_plural_translations
130                  || (distribution != NULL
131                      && distribution->often != NULL
132                      && j < distribution->often_length
133                      && distribution->often[j]
134                      && !(has_range_p (range)
135                           && distribution->histogram (distribution,
136                                                       range.min, range.max, j)
137                              <= 1)));
138
139               if (parser->check (msgid_descr, msgstr_descr,
140                                  strict_checking,
141                                  error_logger, pretty_msgid, pretty_msgstr))
142                 seen_errors++;
143
144               parser->free (msgstr_descr);
145             }
146           else
147             {
148               error_logger (_("\
149 '%s' is not a valid %s format string, unlike '%s'. Reason: %s"),
150                             pretty_msgstr, format_language_pretty[i],
151                             pretty_msgid, invalid_reason);
152               seen_errors++;
153               free (invalid_reason);
154             }
155         }
156
157       parser->free (msgid_descr);
158     }
159   else
160     free (invalid_reason);
161
162   return seen_errors;
163 }
164
165 /* Check whether both formats strings contain compatible format
166    specifications.
167    Return the number of errors that were seen.  */
168 int
169 check_msgid_msgstr_format (const char *msgid, const char *msgid_plural,
170                            const char *msgstr, size_t msgstr_len,
171                            const enum is_format is_format[NFORMATS],
172                            struct argument_range range,
173                            const struct plural_distribution *distribution,
174                            formatstring_error_logger_t error_logger)
175 {
176   int seen_errors = 0;
177   size_t i;
178
179   /* We check only those messages for which the msgid's is_format flag
180      is one of 'yes' or 'possible'.  We don't check msgids with is_format
181      'no' or 'impossible', to obey the programmer's order.  We don't check
182      msgids with is_format 'undecided' because that would introduce too
183      many checks, thus forcing the programmer to add "xgettext: no-c-format"
184      anywhere where a translator wishes to use a percent sign.  */
185   for (i = 0; i < NFORMATS; i++)
186     if (possible_format_p (is_format[i]))
187       seen_errors += check_msgid_msgstr_format_i (msgid, msgid_plural,
188                                                   msgstr, msgstr_len, i,
189                                                   range,
190                                                   distribution,
191                                                   error_logger);
192
193   return seen_errors;
194 }