/src/gettext/gettext-tools/src/format-javascript.c
Line | Count | Source |
1 | | /* JavaScript format strings. |
2 | | Copyright (C) 2001-2026 Free Software Foundation, Inc. |
3 | | |
4 | | This program is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation; either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | This program is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
16 | | |
17 | | /* Written by Bruno Haible, Andreas Stricker, and Daiki Ueno. */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <stdbool.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include "format.h" |
26 | | #include "attribute.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 | 0 | #define _(str) gettext (str) |
34 | | |
35 | | /* JavaScript format strings are not in the language specification, |
36 | | but there are several implementations which provide the printf-like |
37 | | feature. Here, we provide a permissive parser which at least accepts |
38 | | format strings supported by Gjs version 1.40, where: |
39 | | |
40 | | A directive |
41 | | - starts with '%' or '%m$' where m is a positive integer, |
42 | | - is optionally followed by any of the characters '0', '-', ' ', |
43 | | or 'I', each of which acts as a flag, |
44 | | - is optionally followed by a width specification: a nonempty digit |
45 | | sequence, |
46 | | - is optionally followed by '.' and a precision specification: a nonempty |
47 | | digit sequence, |
48 | | - is finished by a specifier |
49 | | - 's', that needs a string argument, |
50 | | - 'b', 'd', 'u', 'o', 'x', 'X', that need an integer argument, |
51 | | - 'f', that need a floating-point argument, |
52 | | - 'c', that needs a character argument. |
53 | | - 'j', that needs an argument of any type. |
54 | | Additionally there is the directive '%%', which takes no argument. */ |
55 | | |
56 | | enum format_arg_type |
57 | | { |
58 | | FAT_NONE, |
59 | | FAT_ANY, |
60 | | FAT_CHARACTER, |
61 | | FAT_STRING, |
62 | | FAT_INTEGER, |
63 | | FAT_FLOAT |
64 | | }; |
65 | | |
66 | | struct numbered_arg |
67 | | { |
68 | | size_t number; |
69 | | enum format_arg_type type; |
70 | | }; |
71 | | |
72 | | struct spec |
73 | | { |
74 | | size_t directives; |
75 | | /* We consider a directive as "likely intentional" if it does not contain a |
76 | | space. This prevents xgettext from flagging strings like "100% complete" |
77 | | as 'javascript-format' if they don't occur in a context that requires a format |
78 | | string. */ |
79 | | size_t likely_intentional_directives; |
80 | | size_t numbered_arg_count; |
81 | | struct numbered_arg *numbered |
82 | | COUNTED_BY (numbered_arg_count); |
83 | | }; |
84 | | |
85 | | |
86 | | static int |
87 | | numbered_arg_compare (const void *p1, const void *p2) |
88 | 0 | { |
89 | 0 | size_t n1 = ((const struct numbered_arg *) p1)->number; |
90 | 0 | size_t n2 = ((const struct numbered_arg *) p2)->number; |
91 | |
|
92 | 0 | return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0); |
93 | 0 | } |
94 | | |
95 | | static void * |
96 | | format_parse (const char *format, bool translated, char *fdi, |
97 | | char **invalid_reason) |
98 | 0 | { |
99 | 0 | const char *const format_start = format; |
100 | |
|
101 | 0 | struct spec spec; |
102 | 0 | spec.directives = 0; |
103 | 0 | spec.likely_intentional_directives = 0; |
104 | 0 | spec.numbered_arg_count = 0; |
105 | 0 | spec.numbered = NULL; |
106 | 0 | size_t numbered_allocated = 0; |
107 | 0 | size_t unnumbered_arg_count = 0; |
108 | 0 | struct numbered_arg *unnumbered = NULL; |
109 | |
|
110 | 0 | for (; *format != '\0';) |
111 | | /* Invariant: spec.numbered_arg_count == 0 || unnumbered_arg_count == 0. */ |
112 | 0 | if (*format++ == '%') |
113 | 0 | { |
114 | | /* A directive. */ |
115 | 0 | FDI_SET (format - 1, FMTDIR_START); |
116 | 0 | spec.directives++; |
117 | 0 | bool likely_intentional = true; |
118 | |
|
119 | 0 | size_t number = 0; |
120 | 0 | if (c_isdigit (*format)) |
121 | 0 | { |
122 | 0 | const char *f = format; |
123 | 0 | size_t m = 0; |
124 | |
|
125 | 0 | do |
126 | 0 | { |
127 | 0 | m = 10 * m + (*f - '0'); |
128 | 0 | f++; |
129 | 0 | } |
130 | 0 | while (c_isdigit (*f)); |
131 | |
|
132 | 0 | if (*f == '$') |
133 | 0 | { |
134 | 0 | if (m == 0) |
135 | 0 | { |
136 | 0 | *invalid_reason = INVALID_ARGNO_0 (spec.directives); |
137 | 0 | FDI_SET (f, FMTDIR_ERROR); |
138 | 0 | goto bad_format; |
139 | 0 | } |
140 | 0 | number = m; |
141 | 0 | format = ++f; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | /* Parse flags. */ |
146 | 0 | while (*format == '-' || *format == '+' || *format == ' ' |
147 | 0 | || *format == '0' || *format == 'I') |
148 | 0 | { |
149 | 0 | if (*format == ' ') |
150 | 0 | likely_intentional = false; |
151 | 0 | format++; |
152 | 0 | } |
153 | | |
154 | | /* Parse width. */ |
155 | 0 | while (c_isdigit (*format)) |
156 | 0 | format++; |
157 | | |
158 | | /* Parse precision. */ |
159 | 0 | if (*format == '.') |
160 | 0 | { |
161 | 0 | format++; |
162 | |
|
163 | 0 | if (!c_isdigit (*format)) |
164 | 0 | { |
165 | 0 | if (*format == '\0') |
166 | 0 | { |
167 | 0 | *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE (); |
168 | 0 | FDI_SET (format - 1, FMTDIR_ERROR); |
169 | 0 | } |
170 | 0 | else |
171 | 0 | { |
172 | 0 | *invalid_reason = INVALID_PRECISION_MISSING (spec.directives); |
173 | 0 | FDI_SET (format, FMTDIR_ERROR); |
174 | 0 | } |
175 | 0 | goto bad_format; |
176 | 0 | } |
177 | | |
178 | 0 | do format++; while (c_isdigit (*format)); |
179 | 0 | } |
180 | | |
181 | 0 | enum format_arg_type type; |
182 | 0 | switch (*format) |
183 | 0 | { |
184 | 0 | case '%': |
185 | 0 | type = FAT_NONE; |
186 | 0 | break; |
187 | 0 | case 'c': |
188 | 0 | type = FAT_CHARACTER; |
189 | 0 | break; |
190 | 0 | case 's': |
191 | 0 | type = FAT_STRING; |
192 | 0 | break; |
193 | 0 | case 'b': case 'd': case 'o': case 'x': case 'X': |
194 | 0 | type = FAT_INTEGER; |
195 | 0 | break; |
196 | 0 | case 'f': |
197 | 0 | type = FAT_FLOAT; |
198 | 0 | break; |
199 | 0 | case 'j': |
200 | 0 | type = FAT_ANY; |
201 | 0 | break; |
202 | 0 | default: |
203 | 0 | if (*format == '\0') |
204 | 0 | { |
205 | 0 | *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE (); |
206 | 0 | FDI_SET (format - 1, FMTDIR_ERROR); |
207 | 0 | } |
208 | 0 | else |
209 | 0 | { |
210 | 0 | *invalid_reason = |
211 | 0 | INVALID_CONVERSION_SPECIFIER (spec.directives, *format); |
212 | 0 | FDI_SET (format, FMTDIR_ERROR); |
213 | 0 | } |
214 | 0 | goto bad_format; |
215 | 0 | } |
216 | | |
217 | 0 | if (type != FAT_NONE) |
218 | 0 | { |
219 | 0 | if (number) |
220 | 0 | { |
221 | | /* Numbered argument. */ |
222 | | |
223 | | /* Numbered and unnumbered specifications are exclusive. */ |
224 | 0 | if (unnumbered_arg_count > 0) |
225 | 0 | { |
226 | 0 | *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED (); |
227 | 0 | FDI_SET (format, FMTDIR_ERROR); |
228 | 0 | goto bad_format; |
229 | 0 | } |
230 | | |
231 | 0 | if (numbered_allocated == spec.numbered_arg_count) |
232 | 0 | { |
233 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
234 | 0 | spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, numbered_allocated * sizeof (struct numbered_arg)); |
235 | 0 | } |
236 | 0 | size_t numbered_index = spec.numbered_arg_count++; |
237 | 0 | spec.numbered[numbered_index].number = number; |
238 | 0 | spec.numbered[numbered_index].type = type; |
239 | 0 | } |
240 | 0 | else |
241 | 0 | { |
242 | | /* Unnumbered argument. */ |
243 | | |
244 | | /* Numbered and unnumbered specifications are exclusive. */ |
245 | 0 | if (spec.numbered_arg_count > 0) |
246 | 0 | { |
247 | 0 | *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED (); |
248 | 0 | FDI_SET (format, FMTDIR_ERROR); |
249 | 0 | goto bad_format; |
250 | 0 | } |
251 | | |
252 | 0 | if (numbered_allocated == unnumbered_arg_count) |
253 | 0 | { |
254 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
255 | 0 | unnumbered = (struct numbered_arg *) xrealloc (unnumbered, numbered_allocated * sizeof (struct numbered_arg)); |
256 | 0 | } |
257 | 0 | size_t unnumbered_index = unnumbered_arg_count++; |
258 | 0 | unnumbered[unnumbered_index].number = unnumbered_index + 1; |
259 | 0 | unnumbered[unnumbered_index].type = type; |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | 0 | if (likely_intentional) |
264 | 0 | spec.likely_intentional_directives++; |
265 | 0 | FDI_SET (format, FMTDIR_END); |
266 | |
|
267 | 0 | format++; |
268 | 0 | } |
269 | | |
270 | | /* Convert the unnumbered argument array to numbered arguments. */ |
271 | 0 | if (unnumbered_arg_count > 0) |
272 | 0 | { |
273 | 0 | spec.numbered = unnumbered; |
274 | 0 | spec.numbered_arg_count = unnumbered_arg_count; |
275 | 0 | } |
276 | | /* Sort the numbered argument array, and eliminate duplicates. */ |
277 | 0 | else if (spec.numbered_arg_count > 1) |
278 | 0 | { |
279 | 0 | qsort (spec.numbered, spec.numbered_arg_count, |
280 | 0 | sizeof (struct numbered_arg), numbered_arg_compare); |
281 | | |
282 | | /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */ |
283 | 0 | bool err = false; |
284 | 0 | size_t i, j; |
285 | 0 | for (i = j = 0; i < spec.numbered_arg_count; i++) |
286 | 0 | if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number) |
287 | 0 | { |
288 | 0 | enum format_arg_type type1 = spec.numbered[i].type; |
289 | 0 | enum format_arg_type type2 = spec.numbered[j-1].type; |
290 | |
|
291 | 0 | enum format_arg_type type_both; |
292 | 0 | if (type1 == type2) |
293 | 0 | type_both = type1; |
294 | 0 | else |
295 | 0 | { |
296 | | /* Incompatible types. */ |
297 | 0 | type_both = FAT_NONE; |
298 | 0 | if (!err) |
299 | 0 | *invalid_reason = |
300 | 0 | INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number); |
301 | 0 | err = true; |
302 | 0 | } |
303 | |
|
304 | 0 | spec.numbered[j-1].type = type_both; |
305 | 0 | } |
306 | 0 | else |
307 | 0 | { |
308 | 0 | if (j < i) |
309 | 0 | { |
310 | 0 | spec.numbered[j].number = spec.numbered[i].number; |
311 | 0 | spec.numbered[j].type = spec.numbered[i].type; |
312 | 0 | } |
313 | 0 | j++; |
314 | 0 | } |
315 | 0 | spec.numbered_arg_count = j; |
316 | 0 | if (err) |
317 | | /* *invalid_reason has already been set above. */ |
318 | 0 | goto bad_format; |
319 | 0 | } |
320 | | |
321 | 0 | struct spec *result = XMALLOC (struct spec); |
322 | 0 | *result = spec; |
323 | 0 | return result; |
324 | | |
325 | 0 | bad_format: |
326 | 0 | if (unnumbered != NULL) |
327 | 0 | free (unnumbered); |
328 | 0 | if (spec.numbered != NULL) |
329 | 0 | free (spec.numbered); |
330 | 0 | return NULL; |
331 | 0 | } |
332 | | |
333 | | static void |
334 | | format_free (void *descr) |
335 | 0 | { |
336 | 0 | struct spec *spec = (struct spec *) descr; |
337 | |
|
338 | 0 | if (spec->numbered != NULL) |
339 | 0 | free (spec->numbered); |
340 | 0 | free (spec); |
341 | 0 | } |
342 | | |
343 | | static int |
344 | | format_get_number_of_directives (void *descr) |
345 | 0 | { |
346 | 0 | struct spec *spec = (struct spec *) descr; |
347 | |
|
348 | 0 | return spec->directives; |
349 | 0 | } |
350 | | |
351 | | static bool |
352 | | format_is_unlikely_intentional (void *descr) |
353 | 0 | { |
354 | 0 | struct spec *spec = (struct spec *) descr; |
355 | |
|
356 | 0 | return spec->likely_intentional_directives == 0; |
357 | 0 | } |
358 | | |
359 | | static bool |
360 | | format_check (void *msgid_descr, void *msgstr_descr, bool equality, |
361 | | formatstring_error_logger_t error_logger, void *error_logger_data, |
362 | | const char *pretty_msgid, const char *pretty_msgstr) |
363 | 0 | { |
364 | 0 | struct spec *spec1 = (struct spec *) msgid_descr; |
365 | 0 | struct spec *spec2 = (struct spec *) msgstr_descr; |
366 | 0 | bool err = false; |
367 | |
|
368 | 0 | if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0) |
369 | 0 | { |
370 | 0 | size_t n1 = spec1->numbered_arg_count; |
371 | 0 | size_t n2 = spec2->numbered_arg_count; |
372 | | |
373 | | /* Check that the argument numbers are the same. |
374 | | Both arrays are sorted. We search for the first difference. */ |
375 | 0 | { |
376 | 0 | size_t i, j; |
377 | 0 | for (i = 0, j = 0; i < n1 || j < n2; ) |
378 | 0 | { |
379 | 0 | int cmp = (i >= n1 ? 1 : |
380 | 0 | j >= n2 ? -1 : |
381 | 0 | spec1->numbered[i].number > spec2->numbered[j].number ? 1 : |
382 | 0 | spec1->numbered[i].number < spec2->numbered[j].number ? -1 : |
383 | 0 | 0); |
384 | 0 | if (cmp > 0) |
385 | 0 | { |
386 | 0 | if (error_logger) |
387 | 0 | error_logger (error_logger_data, |
388 | 0 | _("a format specification for argument %zu, as in '%s', doesn't exist in '%s'"), |
389 | 0 | spec2->numbered[j].number, pretty_msgstr, |
390 | 0 | pretty_msgid); |
391 | 0 | err = true; |
392 | 0 | break; |
393 | 0 | } |
394 | 0 | else if (cmp < 0) |
395 | 0 | { |
396 | 0 | if (equality) |
397 | 0 | { |
398 | 0 | if (error_logger) |
399 | 0 | error_logger (error_logger_data, |
400 | 0 | _("a format specification for argument %zu doesn't exist in '%s'"), |
401 | 0 | spec1->numbered[i].number, pretty_msgstr); |
402 | 0 | err = true; |
403 | 0 | break; |
404 | 0 | } |
405 | 0 | else |
406 | 0 | i++; |
407 | 0 | } |
408 | 0 | else |
409 | 0 | j++, i++; |
410 | 0 | } |
411 | 0 | } |
412 | | /* Check the argument types are the same. */ |
413 | 0 | if (!err) |
414 | 0 | { |
415 | 0 | size_t i, j; |
416 | 0 | for (i = 0, j = 0; j < n2; ) |
417 | 0 | { |
418 | 0 | if (spec1->numbered[i].number == spec2->numbered[j].number) |
419 | 0 | { |
420 | 0 | if (!(spec1->numbered[i].type == spec2->numbered[j].type |
421 | 0 | || (!equality |
422 | 0 | && (spec1->numbered[i].type == FAT_ANY |
423 | 0 | || spec2->numbered[i].type == FAT_ANY)))) |
424 | 0 | { |
425 | 0 | if (error_logger) |
426 | 0 | error_logger (error_logger_data, |
427 | 0 | _("format specifications in '%s' and '%s' for argument %zu are not the same"), |
428 | 0 | pretty_msgid, pretty_msgstr, |
429 | 0 | spec2->numbered[j].number); |
430 | 0 | err = true; |
431 | 0 | break; |
432 | 0 | } |
433 | 0 | j++, i++; |
434 | 0 | } |
435 | 0 | else |
436 | 0 | i++; |
437 | 0 | } |
438 | 0 | } |
439 | 0 | } |
440 | |
|
441 | 0 | return err; |
442 | 0 | } |
443 | | |
444 | | |
445 | | struct formatstring_parser formatstring_javascript = |
446 | | { |
447 | | format_parse, |
448 | | format_free, |
449 | | format_get_number_of_directives, |
450 | | format_is_unlikely_intentional, |
451 | | format_check |
452 | | }; |
453 | | |
454 | | |
455 | | #ifdef TEST |
456 | | |
457 | | /* Test program: Print the argument list specification returned by |
458 | | format_parse for strings read from standard input. */ |
459 | | |
460 | | #include <stdio.h> |
461 | | |
462 | | static void |
463 | | format_print (void *descr) |
464 | | { |
465 | | struct spec *spec = (struct spec *) descr; |
466 | | |
467 | | if (spec == NULL) |
468 | | { |
469 | | printf ("INVALID"); |
470 | | return; |
471 | | } |
472 | | |
473 | | printf ("("); |
474 | | for (size_t i = 0; i < spec->numbered_arg_count; i++) |
475 | | { |
476 | | if (i > 0) |
477 | | printf (" "); |
478 | | switch (spec->numbered[i].type) |
479 | | { |
480 | | case FAT_ANY: |
481 | | printf ("*"); |
482 | | break; |
483 | | case FAT_CHARACTER: |
484 | | printf ("c"); |
485 | | break; |
486 | | case FAT_STRING: |
487 | | printf ("s"); |
488 | | break; |
489 | | case FAT_INTEGER: |
490 | | printf ("i"); |
491 | | break; |
492 | | case FAT_FLOAT: |
493 | | printf ("f"); |
494 | | break; |
495 | | default: |
496 | | abort (); |
497 | | } |
498 | | } |
499 | | printf (")"); |
500 | | } |
501 | | |
502 | | int |
503 | | main () |
504 | | { |
505 | | for (;;) |
506 | | { |
507 | | char *line = NULL; |
508 | | size_t line_size = 0; |
509 | | int line_len = getline (&line, &line_size, stdin); |
510 | | if (line_len < 0) |
511 | | break; |
512 | | if (line_len > 0 && line[line_len - 1] == '\n') |
513 | | line[--line_len] = '\0'; |
514 | | |
515 | | char *invalid_reason = NULL; |
516 | | void *descr = format_parse (line, false, NULL, &invalid_reason); |
517 | | |
518 | | format_print (descr); |
519 | | printf ("\n"); |
520 | | if (descr == NULL) |
521 | | printf ("%s\n", invalid_reason); |
522 | | |
523 | | free (invalid_reason); |
524 | | free (line); |
525 | | } |
526 | | |
527 | | return 0; |
528 | | } |
529 | | |
530 | | /* |
531 | | * For Emacs M-x compile |
532 | | * Local Variables: |
533 | | * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../../gettext-runtime/intl -DTEST format-javascript.c ../gnulib-lib/libgettextlib.la" |
534 | | * End: |
535 | | */ |
536 | | #endif /* TEST */ |