/src/gettext/gettext-tools/src/format-perl.c
Line | Count | Source |
1 | | /* Perl format strings. |
2 | | Copyright (C) 2004-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. */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <stdbool.h> |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include "format.h" |
25 | | #include "c-ctype.h" |
26 | | #include "xalloc.h" |
27 | | #include "xvasprintf.h" |
28 | | #include "format-invalid.h" |
29 | | #include "gettext.h" |
30 | | |
31 | 0 | #define _(str) gettext (str) |
32 | | |
33 | | /* Perl format strings are implemented in function Perl_sv_vcatpvfn in |
34 | | perl-5.8.0/sv.c. |
35 | | A directive |
36 | | - starts with '%' or '%m$' where m is a positive integer starting with a |
37 | | nonzero digit, |
38 | | - is optionally followed by any of the characters '#', '0', '-', ' ', '+', |
39 | | each of which acts as a flag, |
40 | | - is optionally followed by a vector specification: 'v' or '*v' (reads an |
41 | | argument) or '*m$v' where m is a positive integer starting with a nonzero |
42 | | digit, |
43 | | - is optionally followed by a width specification: '*' (reads an argument) |
44 | | or '*m$' where m is a positive integer starting with a nonzero digit or |
45 | | a nonempty digit sequence starting with a nonzero digit, |
46 | | - is optionally followed by '.' and a precision specification: '*' (reads |
47 | | an argument) or '*m$' where m is a positive integer starting with a |
48 | | nonzero digit or an optional nonempty digit sequence, |
49 | | - is optionally followed by a size specifier, one of 'h' 'l' 'll' 'L' 'q' |
50 | | 'V' 'I32' 'I64' 'I', |
51 | | - is finished by a specifier |
52 | | - '%', that needs no argument, |
53 | | - 'c', that needs a small integer argument, |
54 | | - 's', that needs a string argument, |
55 | | - '_', that needs a scalar vector argument, |
56 | | - 'p', that needs a pointer argument, |
57 | | - 'i', 'd', 'D', that need an integer argument, |
58 | | - 'u', 'U', 'b', 'o', 'O', 'x', 'X', that need an unsigned integer |
59 | | argument, |
60 | | - 'e', 'E', 'f', 'F', 'g', 'G', that need a floating-point argument, |
61 | | - 'n', that needs a pointer to integer. |
62 | | So there can be numbered argument specifications: |
63 | | - '%m$' for the format string, |
64 | | - '*m$v' for the vector, |
65 | | - '*m$' for the width, |
66 | | - '.*m$' for the precision. |
67 | | Numbered and unnumbered argument specifications can be used in the same |
68 | | string. The effect of '%m$' is to take argument number m, without affecting |
69 | | the current argument number. The current argument number is incremented |
70 | | after processing a directive with an unnumbered argument specification. |
71 | | */ |
72 | | |
73 | | enum format_arg_type |
74 | | { |
75 | | FAT_NONE = 0, |
76 | | /* Basic types */ |
77 | | FAT_INTEGER = 1, |
78 | | FAT_DOUBLE = 2, |
79 | | FAT_CHAR = 3, |
80 | | FAT_STRING = 4, |
81 | | FAT_SCALAR_VECTOR = 5, |
82 | | FAT_POINTER = 6, |
83 | | FAT_COUNT_POINTER = 7, |
84 | | /* Flags */ |
85 | | FAT_UNSIGNED = 1 << 3, |
86 | | FAT_SIZE_SHORT = 1 << 4, |
87 | | FAT_SIZE_V = 2 << 4, |
88 | | FAT_SIZE_PTR = 3 << 4, |
89 | | FAT_SIZE_LONG = 4 << 4, |
90 | | FAT_SIZE_LONGLONG = 5 << 4, |
91 | | /* Bitmasks */ |
92 | | FAT_SIZE_MASK = (FAT_SIZE_SHORT | FAT_SIZE_V | FAT_SIZE_PTR |
93 | | | FAT_SIZE_LONG | FAT_SIZE_LONGLONG) |
94 | | }; |
95 | | #ifdef __cplusplus |
96 | | typedef int format_arg_type_t; |
97 | | #else |
98 | | typedef enum format_arg_type format_arg_type_t; |
99 | | #endif |
100 | | |
101 | | struct numbered_arg |
102 | | { |
103 | | size_t number; |
104 | | format_arg_type_t type; |
105 | | }; |
106 | | |
107 | | struct spec |
108 | | { |
109 | | size_t directives; |
110 | | /* We consider a directive as "likely intentional" if it does not contain a |
111 | | space. This prevents xgettext from flagging strings like "100% complete" |
112 | | as 'perl-format' if they don't occur in a context that requires a format |
113 | | string. */ |
114 | | size_t likely_intentional_directives; |
115 | | size_t numbered_arg_count; |
116 | | struct numbered_arg *numbered; |
117 | | }; |
118 | | |
119 | | /* Locale independent test for a nonzero decimal digit. */ |
120 | 0 | #define c_isnonzerodigit(c) ((unsigned int) ((c) - '1') < 9) |
121 | | |
122 | | |
123 | | static int |
124 | | numbered_arg_compare (const void *p1, const void *p2) |
125 | 0 | { |
126 | 0 | size_t n1 = ((const struct numbered_arg *) p1)->number; |
127 | 0 | size_t n2 = ((const struct numbered_arg *) p2)->number; |
128 | |
|
129 | 0 | return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0); |
130 | 0 | } |
131 | | |
132 | | static void * |
133 | | format_parse (const char *format, bool translated, char *fdi, |
134 | | char **invalid_reason) |
135 | 0 | { |
136 | 0 | const char *const format_start = format; |
137 | |
|
138 | 0 | size_t directives = 0; |
139 | 0 | size_t likely_intentional_directives = 0; |
140 | 0 | size_t numbered_arg_count = 0; |
141 | 0 | struct numbered_arg *numbered = NULL; |
142 | 0 | size_t numbered_allocated = 0; |
143 | 0 | size_t unnumbered_arg_count = 0; |
144 | |
|
145 | 0 | for (; *format != '\0';) |
146 | 0 | if (*format++ == '%') |
147 | 0 | { |
148 | | /* A directive. */ |
149 | 0 | FDI_SET (format - 1, FMTDIR_START); |
150 | 0 | directives++; |
151 | 0 | bool likely_intentional = true; |
152 | |
|
153 | 0 | size_t number = 0; |
154 | 0 | if (c_isnonzerodigit (*format)) |
155 | 0 | { |
156 | 0 | const char *f = format; |
157 | 0 | size_t m = 0; |
158 | |
|
159 | 0 | do |
160 | 0 | { |
161 | 0 | m = 10 * m + (*f - '0'); |
162 | 0 | f++; |
163 | 0 | } |
164 | 0 | while (c_isdigit (*f)); |
165 | |
|
166 | 0 | if (*f == '$') |
167 | 0 | { |
168 | 0 | number = m; |
169 | 0 | format = ++f; |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | /* Parse flags. */ |
174 | 0 | while (*format == ' ' || *format == '+' || *format == '-' |
175 | 0 | || *format == '#' || *format == '0') |
176 | 0 | { |
177 | 0 | if (*format == ' ') |
178 | 0 | likely_intentional = false; |
179 | 0 | format++; |
180 | 0 | } |
181 | | |
182 | | /* Parse vector. */ |
183 | 0 | bool vectorize = false; |
184 | 0 | if (*format == 'v') |
185 | 0 | { |
186 | 0 | format++; |
187 | 0 | vectorize = true; |
188 | 0 | } |
189 | 0 | else if (*format == '*') |
190 | 0 | { |
191 | 0 | const char *f = format; |
192 | |
|
193 | 0 | f++; |
194 | 0 | if (*f == 'v') |
195 | 0 | { |
196 | 0 | format = ++f; |
197 | 0 | vectorize = true; |
198 | | |
199 | | /* Unnumbered argument. */ |
200 | 0 | if (numbered_allocated == numbered_arg_count) |
201 | 0 | { |
202 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
203 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
204 | 0 | } |
205 | 0 | numbered[numbered_arg_count].number = ++unnumbered_arg_count; |
206 | 0 | numbered[numbered_arg_count].type = FAT_SCALAR_VECTOR; /* or FAT_STRING? */ |
207 | 0 | numbered_arg_count++; |
208 | 0 | } |
209 | 0 | else if (c_isnonzerodigit (*f)) |
210 | 0 | { |
211 | 0 | size_t m = 0; |
212 | |
|
213 | 0 | do |
214 | 0 | { |
215 | 0 | m = 10 * m + (*f - '0'); |
216 | 0 | f++; |
217 | 0 | } |
218 | 0 | while (c_isdigit (*f)); |
219 | |
|
220 | 0 | if (*f == '$') |
221 | 0 | { |
222 | 0 | f++; |
223 | 0 | if (*f == 'v') |
224 | 0 | { |
225 | 0 | size_t vector_number = m; |
226 | |
|
227 | 0 | format = ++f; |
228 | 0 | vectorize = true; |
229 | | |
230 | | /* Numbered argument. */ |
231 | | /* Note: As of perl-5.8.0, this is not correctly |
232 | | implemented in perl's sv.c. */ |
233 | 0 | if (numbered_allocated == numbered_arg_count) |
234 | 0 | { |
235 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
236 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
237 | 0 | } |
238 | 0 | numbered[numbered_arg_count].number = vector_number; |
239 | 0 | numbered[numbered_arg_count].type = FAT_SCALAR_VECTOR; /* or FAT_STRING? */ |
240 | 0 | numbered_arg_count++; |
241 | 0 | } |
242 | 0 | } |
243 | 0 | } |
244 | 0 | } |
245 | |
|
246 | 0 | if (vectorize) |
247 | 0 | { |
248 | | /* Numbered or unnumbered argument. */ |
249 | 0 | if (numbered_allocated == numbered_arg_count) |
250 | 0 | { |
251 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
252 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
253 | 0 | } |
254 | 0 | numbered[numbered_arg_count].number = (number ? number : ++unnumbered_arg_count); |
255 | 0 | numbered[numbered_arg_count].type = FAT_SCALAR_VECTOR; |
256 | 0 | numbered_arg_count++; |
257 | 0 | } |
258 | | |
259 | | /* Parse width. */ |
260 | 0 | if (*format == '*') |
261 | 0 | { |
262 | 0 | format++; |
263 | |
|
264 | 0 | size_t width_number = 0; |
265 | 0 | if (c_isnonzerodigit (*format)) |
266 | 0 | { |
267 | 0 | const char *f = format; |
268 | 0 | size_t m = 0; |
269 | |
|
270 | 0 | do |
271 | 0 | { |
272 | 0 | m = 10 * m + (*f - '0'); |
273 | 0 | f++; |
274 | 0 | } |
275 | 0 | while (c_isdigit (*f)); |
276 | |
|
277 | 0 | if (*f == '$') |
278 | 0 | { |
279 | 0 | width_number = m; |
280 | 0 | format = ++f; |
281 | 0 | } |
282 | 0 | } |
283 | | |
284 | | /* Numbered or unnumbered argument. */ |
285 | | /* Note: As of perl-5.8.0, this is not correctly |
286 | | implemented in perl's sv.c. */ |
287 | 0 | if (numbered_allocated == numbered_arg_count) |
288 | 0 | { |
289 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
290 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
291 | 0 | } |
292 | 0 | numbered[numbered_arg_count].number = (width_number ? width_number : ++unnumbered_arg_count); |
293 | 0 | numbered[numbered_arg_count].type = FAT_INTEGER; |
294 | 0 | numbered_arg_count++; |
295 | 0 | } |
296 | 0 | else if (c_isnonzerodigit (*format)) |
297 | 0 | { |
298 | 0 | do format++; while (c_isdigit (*format)); |
299 | 0 | } |
300 | | |
301 | | /* Parse precision. */ |
302 | 0 | if (*format == '.') |
303 | 0 | { |
304 | 0 | format++; |
305 | |
|
306 | 0 | if (*format == '*') |
307 | 0 | { |
308 | 0 | format++; |
309 | |
|
310 | 0 | size_t precision_number = 0; |
311 | 0 | if (c_isnonzerodigit (*format)) |
312 | 0 | { |
313 | 0 | const char *f = format; |
314 | 0 | size_t m = 0; |
315 | |
|
316 | 0 | do |
317 | 0 | { |
318 | 0 | m = 10 * m + (*f - '0'); |
319 | 0 | f++; |
320 | 0 | } |
321 | 0 | while (c_isdigit (*f)); |
322 | |
|
323 | 0 | if (*f == '$') |
324 | 0 | { |
325 | 0 | precision_number = m; |
326 | 0 | format = ++f; |
327 | 0 | } |
328 | 0 | } |
329 | | |
330 | | /* Numbered or unnumbered argument. */ |
331 | 0 | if (numbered_allocated == numbered_arg_count) |
332 | 0 | { |
333 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
334 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
335 | 0 | } |
336 | 0 | numbered[numbered_arg_count].number = (precision_number ? precision_number : ++unnumbered_arg_count); |
337 | 0 | numbered[numbered_arg_count].type = FAT_INTEGER; |
338 | 0 | numbered_arg_count++; |
339 | 0 | } |
340 | 0 | else |
341 | 0 | { |
342 | 0 | while (c_isdigit (*format)) format++; |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | /* Parse size. */ |
347 | 0 | format_arg_type_t size = 0; |
348 | 0 | if (*format == 'h') |
349 | 0 | { |
350 | 0 | size = FAT_SIZE_SHORT; |
351 | 0 | format++; |
352 | 0 | } |
353 | 0 | else if (*format == 'l') |
354 | 0 | { |
355 | 0 | if (format[1] == 'l') |
356 | 0 | { |
357 | 0 | size = FAT_SIZE_LONGLONG; |
358 | 0 | format += 2; |
359 | 0 | } |
360 | 0 | else |
361 | 0 | { |
362 | 0 | size = FAT_SIZE_LONG; |
363 | 0 | format++; |
364 | 0 | } |
365 | 0 | } |
366 | 0 | else if (*format == 'L' || *format == 'q') |
367 | 0 | { |
368 | 0 | size = FAT_SIZE_LONGLONG; |
369 | 0 | format++; |
370 | 0 | } |
371 | 0 | else if (*format == 'V') |
372 | 0 | { |
373 | 0 | size = FAT_SIZE_V; |
374 | 0 | format++; |
375 | 0 | } |
376 | 0 | else if (*format == 'I') |
377 | 0 | { |
378 | 0 | if (format[1] == '6' && format[2] == '4') |
379 | 0 | { |
380 | 0 | size = FAT_SIZE_LONGLONG; |
381 | 0 | format += 3; |
382 | 0 | } |
383 | 0 | else if (format[1] == '3' && format[2] == '2') |
384 | 0 | { |
385 | 0 | size = 0; /* FAT_SIZE_INT */ |
386 | 0 | format += 3; |
387 | 0 | } |
388 | 0 | else |
389 | 0 | { |
390 | 0 | size = FAT_SIZE_PTR; |
391 | 0 | format++; |
392 | 0 | } |
393 | 0 | } |
394 | |
|
395 | 0 | format_arg_type_t type; |
396 | 0 | switch (*format) |
397 | 0 | { |
398 | 0 | case '%': |
399 | 0 | type = FAT_NONE; |
400 | 0 | break; |
401 | 0 | case 'c': |
402 | 0 | type = FAT_CHAR; |
403 | 0 | break; |
404 | 0 | case 's': |
405 | 0 | type = FAT_STRING; |
406 | 0 | break; |
407 | 0 | case '_': |
408 | 0 | type = FAT_SCALAR_VECTOR; |
409 | 0 | break; |
410 | 0 | case 'D': |
411 | 0 | type = FAT_INTEGER | FAT_SIZE_V; |
412 | 0 | break; |
413 | 0 | case 'i': case 'd': |
414 | 0 | type = FAT_INTEGER | size; |
415 | 0 | break; |
416 | 0 | case 'U': case 'O': |
417 | 0 | type = FAT_INTEGER | FAT_UNSIGNED | FAT_SIZE_V; |
418 | 0 | break; |
419 | 0 | case 'u': case 'b': case 'o': case 'x': case 'X': |
420 | 0 | type = FAT_INTEGER | FAT_UNSIGNED | size; |
421 | 0 | break; |
422 | 0 | case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': |
423 | 0 | if (size == FAT_SIZE_SHORT || size == FAT_SIZE_LONG) |
424 | 0 | { |
425 | 0 | *invalid_reason = |
426 | 0 | xasprintf (_("In the directive number %zu, the size specifier is incompatible with the conversion specifier '%c'."), directives, *format); |
427 | 0 | FDI_SET (format, FMTDIR_ERROR); |
428 | 0 | goto bad_format; |
429 | 0 | } |
430 | 0 | type = FAT_DOUBLE | size; |
431 | 0 | break; |
432 | 0 | case 'p': |
433 | 0 | type = FAT_POINTER; |
434 | 0 | break; |
435 | 0 | case 'n': |
436 | 0 | type = FAT_COUNT_POINTER | size; |
437 | 0 | break; |
438 | 0 | default: |
439 | 0 | if (*format == '\0') |
440 | 0 | { |
441 | 0 | *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE (); |
442 | 0 | FDI_SET (format - 1, FMTDIR_ERROR); |
443 | 0 | } |
444 | 0 | else |
445 | 0 | { |
446 | 0 | *invalid_reason = |
447 | 0 | INVALID_CONVERSION_SPECIFIER (directives, *format); |
448 | 0 | FDI_SET (format, FMTDIR_ERROR); |
449 | 0 | } |
450 | 0 | goto bad_format; |
451 | 0 | } |
452 | | |
453 | 0 | if (type != FAT_NONE && !vectorize) |
454 | 0 | { |
455 | | /* Numbered or unnumbered argument. */ |
456 | 0 | if (numbered_allocated == numbered_arg_count) |
457 | 0 | { |
458 | 0 | numbered_allocated = 2 * numbered_allocated + 1; |
459 | 0 | numbered = (struct numbered_arg *) xrealloc (numbered, numbered_allocated * sizeof (struct numbered_arg)); |
460 | 0 | } |
461 | 0 | numbered[numbered_arg_count].number = (number ? number : ++unnumbered_arg_count); |
462 | 0 | numbered[numbered_arg_count].type = type; |
463 | 0 | numbered_arg_count++; |
464 | 0 | } |
465 | |
|
466 | 0 | if (likely_intentional) |
467 | 0 | likely_intentional_directives++; |
468 | 0 | FDI_SET (format, FMTDIR_END); |
469 | |
|
470 | 0 | format++; |
471 | 0 | } |
472 | | |
473 | | /* Sort the numbered argument array, and eliminate duplicates. */ |
474 | 0 | if (numbered_arg_count > 1) |
475 | 0 | { |
476 | 0 | qsort (numbered, numbered_arg_count, |
477 | 0 | sizeof (struct numbered_arg), numbered_arg_compare); |
478 | | |
479 | | /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */ |
480 | 0 | bool err = false; |
481 | 0 | size_t i, j; |
482 | 0 | for (i = j = 0; i < numbered_arg_count; i++) |
483 | 0 | if (j > 0 && numbered[i].number == numbered[j-1].number) |
484 | 0 | { |
485 | 0 | format_arg_type_t type1 = numbered[i].type; |
486 | 0 | format_arg_type_t type2 = numbered[j-1].type; |
487 | |
|
488 | 0 | format_arg_type_t type_both; |
489 | 0 | if (type1 == type2) |
490 | 0 | type_both = type1; |
491 | 0 | else |
492 | 0 | { |
493 | | /* Incompatible types. */ |
494 | 0 | type_both = FAT_NONE; |
495 | 0 | if (!err) |
496 | 0 | *invalid_reason = |
497 | 0 | INVALID_INCOMPATIBLE_ARG_TYPES (numbered[i].number); |
498 | 0 | err = true; |
499 | 0 | } |
500 | |
|
501 | 0 | numbered[j-1].type = type_both; |
502 | 0 | } |
503 | 0 | else |
504 | 0 | { |
505 | 0 | if (j < i) |
506 | 0 | { |
507 | 0 | numbered[j].number = numbered[i].number; |
508 | 0 | numbered[j].type = numbered[i].type; |
509 | 0 | } |
510 | 0 | j++; |
511 | 0 | } |
512 | 0 | numbered_arg_count = j; |
513 | 0 | if (err) |
514 | | /* *invalid_reason has already been set above. */ |
515 | 0 | goto bad_format; |
516 | 0 | } |
517 | | |
518 | 0 | struct spec *result = XMALLOC (struct spec); |
519 | 0 | result->directives = directives; |
520 | 0 | result->likely_intentional_directives = likely_intentional_directives; |
521 | 0 | result->numbered_arg_count = numbered_arg_count; |
522 | 0 | result->numbered = numbered; |
523 | 0 | return result; |
524 | | |
525 | 0 | bad_format: |
526 | 0 | if (numbered != NULL) |
527 | 0 | free (numbered); |
528 | 0 | return NULL; |
529 | 0 | } |
530 | | |
531 | | static void |
532 | | format_free (void *descr) |
533 | 0 | { |
534 | 0 | struct spec *spec = (struct spec *) descr; |
535 | |
|
536 | 0 | if (spec->numbered != NULL) |
537 | 0 | free (spec->numbered); |
538 | 0 | free (spec); |
539 | 0 | } |
540 | | |
541 | | static int |
542 | | format_get_number_of_directives (void *descr) |
543 | 0 | { |
544 | 0 | struct spec *spec = (struct spec *) descr; |
545 | |
|
546 | 0 | return spec->directives; |
547 | 0 | } |
548 | | |
549 | | static bool |
550 | | format_is_unlikely_intentional (void *descr) |
551 | 0 | { |
552 | 0 | struct spec *spec = (struct spec *) descr; |
553 | |
|
554 | 0 | return spec->likely_intentional_directives == 0; |
555 | 0 | } |
556 | | |
557 | | static bool |
558 | | format_check (void *msgid_descr, void *msgstr_descr, bool equality, |
559 | | formatstring_error_logger_t error_logger, void *error_logger_data, |
560 | | const char *pretty_msgid, const char *pretty_msgstr) |
561 | 0 | { |
562 | 0 | struct spec *spec1 = (struct spec *) msgid_descr; |
563 | 0 | struct spec *spec2 = (struct spec *) msgstr_descr; |
564 | 0 | bool err = false; |
565 | |
|
566 | 0 | if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0) |
567 | 0 | { |
568 | 0 | size_t n1 = spec1->numbered_arg_count; |
569 | 0 | size_t n2 = spec2->numbered_arg_count; |
570 | | |
571 | | /* Check that the argument numbers are the same. |
572 | | Both arrays are sorted. We search for the first difference. */ |
573 | 0 | { |
574 | 0 | size_t i, j; |
575 | 0 | for (i = 0, j = 0; i < n1 || j < n2; ) |
576 | 0 | { |
577 | 0 | int cmp = (i >= n1 ? 1 : |
578 | 0 | j >= n2 ? -1 : |
579 | 0 | spec1->numbered[i].number > spec2->numbered[j].number ? 1 : |
580 | 0 | spec1->numbered[i].number < spec2->numbered[j].number ? -1 : |
581 | 0 | 0); |
582 | |
|
583 | 0 | if (cmp > 0) |
584 | 0 | { |
585 | 0 | if (error_logger) |
586 | 0 | error_logger (error_logger_data, |
587 | 0 | _("a format specification for argument %zu, as in '%s', doesn't exist in '%s'"), |
588 | 0 | spec2->numbered[j].number, pretty_msgstr, |
589 | 0 | pretty_msgid); |
590 | 0 | err = true; |
591 | 0 | break; |
592 | 0 | } |
593 | 0 | else if (cmp < 0) |
594 | 0 | { |
595 | 0 | if (equality) |
596 | 0 | { |
597 | 0 | if (error_logger) |
598 | 0 | error_logger (error_logger_data, |
599 | 0 | _("a format specification for argument %zu doesn't exist in '%s'"), |
600 | 0 | spec1->numbered[i].number, pretty_msgstr); |
601 | 0 | err = true; |
602 | 0 | break; |
603 | 0 | } |
604 | 0 | else |
605 | 0 | i++; |
606 | 0 | } |
607 | 0 | else |
608 | 0 | j++, i++; |
609 | 0 | } |
610 | 0 | } |
611 | | /* Check the argument types are the same. */ |
612 | 0 | if (!err) |
613 | 0 | { |
614 | 0 | size_t i, j; |
615 | 0 | for (i = 0, j = 0; j < n2; ) |
616 | 0 | { |
617 | 0 | if (spec1->numbered[i].number == spec2->numbered[j].number) |
618 | 0 | { |
619 | 0 | if (spec1->numbered[i].type != spec2->numbered[j].type) |
620 | 0 | { |
621 | 0 | if (error_logger) |
622 | 0 | error_logger (error_logger_data, |
623 | 0 | _("format specifications in '%s' and '%s' for argument %zu are not the same"), |
624 | 0 | pretty_msgid, pretty_msgstr, |
625 | 0 | spec2->numbered[j].number); |
626 | 0 | err = true; |
627 | 0 | break; |
628 | 0 | } |
629 | 0 | j++, i++; |
630 | 0 | } |
631 | 0 | else |
632 | 0 | i++; |
633 | 0 | } |
634 | 0 | } |
635 | 0 | } |
636 | |
|
637 | 0 | return err; |
638 | 0 | } |
639 | | |
640 | | |
641 | | struct formatstring_parser formatstring_perl = |
642 | | { |
643 | | format_parse, |
644 | | format_free, |
645 | | format_get_number_of_directives, |
646 | | format_is_unlikely_intentional, |
647 | | format_check |
648 | | }; |
649 | | |
650 | | |
651 | | #ifdef TEST |
652 | | |
653 | | /* Test program: Print the argument list specification returned by |
654 | | format_parse for strings read from standard input. */ |
655 | | |
656 | | #include <stdio.h> |
657 | | |
658 | | static void |
659 | | format_print (void *descr) |
660 | | { |
661 | | struct spec *spec = (struct spec *) descr; |
662 | | |
663 | | if (spec == NULL) |
664 | | { |
665 | | printf ("INVALID"); |
666 | | return; |
667 | | } |
668 | | |
669 | | printf ("("); |
670 | | size_t last = 1; |
671 | | for (size_t i = 0; i < spec->numbered_arg_count; i++) |
672 | | { |
673 | | size_t number = spec->numbered[i].number; |
674 | | |
675 | | if (i > 0) |
676 | | printf (" "); |
677 | | if (number < last) |
678 | | abort (); |
679 | | for (; last < number; last++) |
680 | | printf ("_ "); |
681 | | if (spec->numbered[i].type & FAT_UNSIGNED) |
682 | | printf ("[unsigned]"); |
683 | | switch (spec->numbered[i].type & FAT_SIZE_MASK) |
684 | | { |
685 | | case 0: |
686 | | break; |
687 | | case FAT_SIZE_SHORT: |
688 | | printf ("[short]"); |
689 | | break; |
690 | | case FAT_SIZE_V: |
691 | | printf ("[IV]"); |
692 | | break; |
693 | | case FAT_SIZE_PTR: |
694 | | printf ("[PTR]"); |
695 | | break; |
696 | | case FAT_SIZE_LONG: |
697 | | printf ("[long]"); |
698 | | break; |
699 | | case FAT_SIZE_LONGLONG: |
700 | | printf ("[long long]"); |
701 | | break; |
702 | | default: |
703 | | abort (); |
704 | | } |
705 | | switch (spec->numbered[i].type & ~(FAT_UNSIGNED | FAT_SIZE_MASK)) |
706 | | { |
707 | | case FAT_INTEGER: |
708 | | printf ("i"); |
709 | | break; |
710 | | case FAT_DOUBLE: |
711 | | printf ("f"); |
712 | | break; |
713 | | case FAT_CHAR: |
714 | | printf ("c"); |
715 | | break; |
716 | | case FAT_STRING: |
717 | | printf ("s"); |
718 | | break; |
719 | | case FAT_SCALAR_VECTOR: |
720 | | printf ("sv"); |
721 | | break; |
722 | | case FAT_POINTER: |
723 | | printf ("p"); |
724 | | break; |
725 | | case FAT_COUNT_POINTER: |
726 | | printf ("n"); |
727 | | break; |
728 | | default: |
729 | | abort (); |
730 | | } |
731 | | last = number + 1; |
732 | | } |
733 | | printf (")"); |
734 | | } |
735 | | |
736 | | int |
737 | | main () |
738 | | { |
739 | | for (;;) |
740 | | { |
741 | | char *line = NULL; |
742 | | size_t line_size = 0; |
743 | | int line_len = getline (&line, &line_size, stdin); |
744 | | if (line_len < 0) |
745 | | break; |
746 | | if (line_len > 0 && line[line_len - 1] == '\n') |
747 | | line[--line_len] = '\0'; |
748 | | |
749 | | char *invalid_reason = NULL; |
750 | | void *descr = format_parse (line, false, NULL, &invalid_reason); |
751 | | |
752 | | format_print (descr); |
753 | | printf ("\n"); |
754 | | if (descr == NULL) |
755 | | printf ("%s\n", invalid_reason); |
756 | | |
757 | | free (invalid_reason); |
758 | | free (line); |
759 | | } |
760 | | |
761 | | return 0; |
762 | | } |
763 | | |
764 | | /* |
765 | | * For Emacs M-x compile |
766 | | * Local Variables: |
767 | | * 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-perl.c ../gnulib-lib/libgettextlib.la" |
768 | | * End: |
769 | | */ |
770 | | |
771 | | #endif /* TEST */ |