1/* Format strings. 2 Copyright (C) 2001-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 19#ifdef HAVE_CONFIG_H 20# include <config.h> 21#endif 22 23/* Specification. */ 24#include "format.h" 25 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. */ 35struct 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_qt */ &formatstring_qt 57}; 58 59/* Check whether both formats strings contain compatible format 60 specifications. Return true if there is an error. */ 61bool 62check_msgid_msgstr_format (const char *msgid, const char *msgid_plural, 63 const char *msgstr, size_t msgstr_len, 64 enum is_format is_format[NFORMATS], 65 formatstring_error_logger_t error_logger) 66{ 67 bool err = false; 68 size_t i; 69 unsigned int j; 70 71 /* We check only those messages for which the msgid's is_format flag 72 is one of 'yes' or 'possible'. We don't check msgids with is_format 73 'no' or 'impossible', to obey the programmer's order. We don't check 74 msgids with is_format 'undecided' because that would introduce too 75 many checks, thus forcing the programmer to add "xgettext: no-c-format" 76 anywhere where a translator wishes to use a percent sign. */ 77 for (i = 0; i < NFORMATS; i++) 78 if (possible_format_p (is_format[i])) 79 { 80 /* At runtime, we can assume the program passes arguments that 81 fit well for msgid. We must signal an error if msgstr wants 82 more arguments that msgid accepts. 83 If msgstr wants fewer arguments than msgid, it wouldn't lead 84 to a crash at runtime, but we nevertheless give an error because 85 1) this situation occurs typically after the programmer has 86 added some arguments to msgid, so we must make the translator 87 specially aware of it (more than just "fuzzy"), 88 2) it is generally wrong if a translation wants to ignore 89 arguments that are used by other translations. */ 90 91 struct formatstring_parser *parser = formatstring_parsers[i]; 92 char *invalid_reason = NULL; 93 void *msgid_descr = 94 parser->parse (msgid_plural != NULL ? msgid_plural : msgid, 95 false, &invalid_reason); 96 97 if (msgid_descr != NULL) 98 { 99 char buf[18+1]; 100 const char *pretty_msgstr = "msgstr"; 101 const char *p_end = msgstr + msgstr_len; 102 const char *p; 103 104 for (p = msgstr, j = 0; p < p_end; p += strlen (p) + 1, j++) 105 { 106 void *msgstr_descr; 107 108 if (msgid_plural != NULL) 109 { 110 sprintf (buf, "msgstr[%u]", j); 111 pretty_msgstr = buf; 112 } 113 114 msgstr_descr = parser->parse (p, true, &invalid_reason); 115 116 if (msgstr_descr != NULL) 117 { 118 if (parser->check (msgid_descr, msgstr_descr, 119 msgid_plural == NULL, 120 error_logger, pretty_msgstr)) 121 err = true; 122 123 parser->free (msgstr_descr); 124 } 125 else 126 { 127 error_logger (_("\ 128'%s' is not a valid %s format string, unlike 'msgid'. Reason: %s"), 129 pretty_msgstr, format_language_pretty[i], 130 invalid_reason); 131 err = true; 132 free (invalid_reason); 133 } 134 } 135 136 parser->free (msgid_descr); 137 } 138 else 139 free (invalid_reason); 140 } 141 142 return err; 143} 144