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