Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / src / format-javascript.c
1 /* JavaScript format strings.
2    Copyright (C) 2001-2004, 2006-2009, 2013, 2015 Free Software
3    Foundation, Inc.
4    Written by Andreas Stricker <andy@knitter.ch>, 2010.
5    It's based on python format module from Bruno Haible.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "format.h"
29 #include "c-ctype.h"
30 #include "xalloc.h"
31 #include "xvasprintf.h"
32 #include "format-invalid.h"
33 #include "gettext.h"
34
35 #define _(str) gettext (str)
36
37 /* Although JavaScript specification itself does not define any format
38    strings, many implementations provide printf-like functions.
39    We provide a permissive parser which accepts commonly used format
40    strings, where:
41
42    A directive
43    - starts with '%',
44    - is optionally followed by any of the characters '0', '-', ' ',
45      or, each of which acts as a flag,
46    - is optionally followed by a width specification: a nonempty digit
47      sequence,
48    - is optionally followed by '.' and a precision specification: a nonempty
49      digit sequence,
50    - is finished by a specifier
51        - 's', that needs a string argument,
52        - 'b', 'd', 'u', 'o', 'x', 'X', that need an integer argument,
53        - 'f', that need a floating-point argument,
54        - 'c', that needs a character argument.
55        - 'j', that needs an argument of any type.
56    Additionally there is the directive '%%', which takes no argument.  */
57
58 enum format_arg_type
59 {
60   FAT_NONE,
61   FAT_ANY,
62   FAT_CHARACTER,
63   FAT_STRING,
64   FAT_INTEGER,
65   FAT_FLOAT
66 };
67
68 struct spec
69 {
70   unsigned int directives;
71   unsigned int format_args_count;
72   unsigned int allocated;
73   enum format_arg_type *format_args;
74 };
75
76 /* Locale independent test for a decimal digit.
77    Argument can be  'char' or 'unsigned char'.  (Whereas the argument of
78    <ctype.h> isdigit must be an 'unsigned char'.)  */
79 #undef isdigit
80 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
81
82
83 static void *
84 format_parse (const char *format, bool translated, char *fdi,
85               char **invalid_reason)
86 {
87   const char *const format_start = format;
88   struct spec spec;
89   struct spec *result;
90
91   spec.directives = 0;
92   spec.format_args_count = 0;
93   spec.allocated = 0;
94   spec.format_args = NULL;
95
96   for (; *format != '\0';)
97     if (*format++ == '%')
98       {
99         /* A directive.  */
100         enum format_arg_type type;
101
102         FDI_SET (format - 1, FMTDIR_START);
103         spec.directives++;
104
105         while (*format == '-' || *format == '+' || *format == ' '
106                || *format == '0' || *format == 'I')
107           format++;
108
109         while (isdigit (*format))
110           format++;
111
112         if (*format == '.')
113           {
114             format++;
115
116             while (isdigit (*format))
117               format++;
118           }
119
120         switch (*format)
121           {
122           case '%':
123             type = FAT_NONE;
124             break;
125           case 'c':
126             type = FAT_CHARACTER;
127             break;
128           case 's':
129             type = FAT_STRING;
130             break;
131           case 'b': case 'd': case 'o': case 'x': case 'X':
132             type = FAT_INTEGER;
133             break;
134           case 'f':
135             type = FAT_FLOAT;
136             break;
137           case 'j':
138             type = FAT_ANY;
139             break;
140           default:
141             if (*format == '\0')
142               {
143                 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
144                 FDI_SET (format - 1, FMTDIR_ERROR);
145               }
146             else
147               {
148                 *invalid_reason =
149                   INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
150                 FDI_SET (format, FMTDIR_ERROR);
151               }
152             goto bad_format;
153           }
154
155         if (*format != '%')
156           {
157             if (spec.allocated == spec.format_args_count)
158               {
159                 spec.allocated = 2 * spec.allocated + 1;
160                 spec.format_args = (enum format_arg_type *) xrealloc (spec.format_args, spec.allocated * sizeof (enum format_arg_type));
161               }
162             spec.format_args[spec.format_args_count] = type;
163             spec.format_args_count++;
164           }
165
166         FDI_SET (format, FMTDIR_END);
167
168         format++;
169       }
170
171   result = XMALLOC (struct spec);
172   *result = spec;
173   return result;
174
175  bad_format:
176   if (spec.format_args != NULL)
177     free (spec.format_args);
178   return NULL;
179 }
180
181 static void
182 format_free (void *descr)
183 {
184   struct spec *spec = (struct spec *) descr;
185
186   if (spec->format_args != NULL)
187     free (spec->format_args);
188   free (spec);
189 }
190
191 static int
192 format_get_number_of_directives (void *descr)
193 {
194   struct spec *spec = (struct spec *) descr;
195
196   return spec->directives;
197 }
198
199 static bool
200 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
201               formatstring_error_logger_t error_logger,
202               const char *pretty_msgid, const char *pretty_msgstr)
203 {
204   struct spec *spec1 = (struct spec *) msgid_descr;
205   struct spec *spec2 = (struct spec *) msgstr_descr;
206   bool err = false;
207
208   if (spec1->format_args_count + spec2->format_args_count > 0)
209     {
210       unsigned int i;
211
212       /* Check the argument types are the same.  */
213       if (spec1->format_args_count != spec2->format_args_count)
214         {
215           if (error_logger)
216             error_logger (_("number of format specifications in '%s' and '%s' does not match"),
217                           pretty_msgid, pretty_msgstr);
218           err = true;
219         }
220       else
221         for (i = 0; i < spec2->format_args_count; i++)
222           if (!(spec1->format_args[i] == spec2->format_args[i]
223                 || (!equality
224                     && (spec1->format_args[i] == FAT_ANY
225                         || spec2->format_args[i] == FAT_ANY))))
226             {
227               if (error_logger)
228                 error_logger (_("format specifications in '%s' and '%s' for argument %u are not the same"),
229                               pretty_msgid, pretty_msgstr, i + 1);
230               err = true;
231             }
232     }
233
234   return err;
235 }
236
237
238 struct formatstring_parser formatstring_javascript =
239 {
240   format_parse,
241   format_free,
242   format_get_number_of_directives,
243   NULL,
244   format_check
245 };
246
247
248 #ifdef TEST
249
250 /* Test program: Print the argument list specification returned by
251    format_parse for strings read from standard input.  */
252
253 #include <stdio.h>
254
255 static void
256 format_print (void *descr)
257 {
258   struct spec *spec = (struct spec *) descr;
259   unsigned int i;
260
261   if (spec == NULL)
262     {
263       printf ("INVALID");
264       return;
265     }
266
267       printf ("(");
268       for (i = 0; i < spec->format_args_count; i++)
269         {
270           if (i > 0)
271             printf (" ");
272           switch (spec->format_args[i])
273             {
274             case FAT_ANY:
275               printf ("*");
276               break;
277             case FAT_CHARACTER:
278               printf ("c");
279               break;
280             case FAT_STRING:
281               printf ("s");
282               break;
283             case FAT_INTEGER:
284               printf ("i");
285               break;
286             case FAT_FLOAT:
287               printf ("f");
288               break;
289             default:
290               abort ();
291             }
292         }
293       printf (")");
294 }
295
296 int
297 main ()
298 {
299   for (;;)
300     {
301       char *line = NULL;
302       size_t line_size = 0;
303       int line_len;
304       char *invalid_reason;
305       void *descr;
306
307       line_len = getline (&line, &line_size, stdin);
308       if (line_len < 0)
309         break;
310       if (line_len > 0 && line[line_len - 1] == '\n')
311         line[--line_len] = '\0';
312
313       invalid_reason = NULL;
314       descr = format_parse (line, false, NULL, &invalid_reason);
315
316       format_print (descr);
317       printf ("\n");
318       if (descr == NULL)
319         printf ("%s\n", invalid_reason);
320
321       free (invalid_reason);
322       free (line);
323     }
324
325   return 0;
326 }
327
328 /*
329  * For Emacs M-x compile
330  * Local Variables:
331  * 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-javascript.c ../gnulib-lib/libgettextlib.la"
332  * End:
333  */
334 #endif /* TEST */