/src/gettext/gettext-tools/src/format-perl-brace.c
Line | Count | Source |
1 | | /* Perl brace 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 | | #include <string.h> |
24 | | |
25 | | #include "format.h" |
26 | | #include "attribute.h" |
27 | | #include "xalloc.h" |
28 | | #include "gettext.h" |
29 | | |
30 | 0 | #define _(str) gettext (str) |
31 | | |
32 | | /* Perl brace format strings are supported by Guido Flohr's libintl-perl |
33 | | package, more precisely by the __expand and __x functions therein. |
34 | | A format string directive here consists of |
35 | | - an opening brace '{', |
36 | | - an identifier [_A-Za-z][_0-9A-Za-z]*, |
37 | | - a closing brace '}'. |
38 | | */ |
39 | | |
40 | | struct named_arg |
41 | | { |
42 | | char *name; |
43 | | }; |
44 | | |
45 | | struct spec |
46 | | { |
47 | | size_t directives; |
48 | | size_t named_arg_count; |
49 | | struct named_arg *named |
50 | | COUNTED_BY (named_arg_count); |
51 | | }; |
52 | | |
53 | | |
54 | | static int |
55 | | named_arg_compare (const void *p1, const void *p2) |
56 | 0 | { |
57 | 0 | return strcmp (((const struct named_arg *) p1)->name, |
58 | 0 | ((const struct named_arg *) p2)->name); |
59 | 0 | } |
60 | | |
61 | | static void * |
62 | | format_parse (const char *format, bool translated, char *fdi, |
63 | | char **invalid_reason) |
64 | 0 | { |
65 | 0 | const char *const format_start = format; |
66 | |
|
67 | 0 | struct spec spec; |
68 | 0 | spec.directives = 0; |
69 | 0 | spec.named_arg_count = 0; |
70 | 0 | spec.named = NULL; |
71 | 0 | size_t named_allocated = 0; |
72 | |
|
73 | 0 | for (; *format != '\0';) |
74 | 0 | if (*format++ == '{') |
75 | 0 | { |
76 | 0 | const char *f = format; |
77 | 0 | char c; |
78 | |
|
79 | 0 | c = *f; |
80 | 0 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') |
81 | 0 | { |
82 | 0 | do |
83 | 0 | c = *++f; |
84 | 0 | while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' |
85 | 0 | || (c >= '0' && c <= '9')); |
86 | 0 | if (c == '}') |
87 | 0 | { |
88 | | /* A directive. */ |
89 | 0 | FDI_SET (format - 1, FMTDIR_START); |
90 | |
|
91 | 0 | const char *name_start = format; |
92 | 0 | const char *name_end = f; |
93 | 0 | size_t n = name_end - name_start; |
94 | |
|
95 | 0 | char *name = XNMALLOC (n + 1, char); |
96 | 0 | memcpy (name, name_start, n); |
97 | 0 | name[n] = '\0'; |
98 | |
|
99 | 0 | spec.directives++; |
100 | |
|
101 | 0 | if (named_allocated == spec.named_arg_count) |
102 | 0 | { |
103 | 0 | named_allocated = 2 * named_allocated + 1; |
104 | 0 | spec.named = (struct named_arg *) xrealloc (spec.named, named_allocated * sizeof (struct named_arg)); |
105 | 0 | } |
106 | 0 | size_t named_index = spec.named_arg_count++; |
107 | 0 | spec.named[named_index].name = name; |
108 | |
|
109 | 0 | FDI_SET (f, FMTDIR_END); |
110 | |
|
111 | 0 | format = ++f; |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | /* Sort the named argument array, and eliminate duplicates. */ |
117 | 0 | if (spec.named_arg_count > 1) |
118 | 0 | { |
119 | 0 | qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg), |
120 | 0 | named_arg_compare); |
121 | | |
122 | | /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */ |
123 | 0 | size_t i, j; |
124 | 0 | for (i = j = 0; i < spec.named_arg_count; i++) |
125 | 0 | if (j > 0 && streq (spec.named[i].name, spec.named[j-1].name)) |
126 | 0 | free (spec.named[i].name); |
127 | 0 | else |
128 | 0 | { |
129 | 0 | if (j < i) |
130 | 0 | spec.named[j].name = spec.named[i].name; |
131 | 0 | j++; |
132 | 0 | } |
133 | 0 | spec.named_arg_count = j; |
134 | 0 | } |
135 | |
|
136 | 0 | struct spec *result = XMALLOC (struct spec); |
137 | 0 | *result = spec; |
138 | 0 | return result; |
139 | 0 | } |
140 | | |
141 | | static void |
142 | | format_free (void *descr) |
143 | 0 | { |
144 | 0 | struct spec *spec = (struct spec *) descr; |
145 | |
|
146 | 0 | if (spec->named != NULL) |
147 | 0 | { |
148 | 0 | for (size_t i = 0; i < spec->named_arg_count; i++) |
149 | 0 | free (spec->named[i].name); |
150 | 0 | free (spec->named); |
151 | 0 | } |
152 | 0 | free (spec); |
153 | 0 | } |
154 | | |
155 | | static int |
156 | | format_get_number_of_directives (void *descr) |
157 | 0 | { |
158 | 0 | struct spec *spec = (struct spec *) descr; |
159 | |
|
160 | 0 | return spec->directives; |
161 | 0 | } |
162 | | |
163 | | static bool |
164 | | format_check (void *msgid_descr, void *msgstr_descr, bool equality, |
165 | | formatstring_error_logger_t error_logger, void *error_logger_data, |
166 | | const char *pretty_msgid, const char *pretty_msgstr) |
167 | 0 | { |
168 | 0 | struct spec *spec1 = (struct spec *) msgid_descr; |
169 | 0 | struct spec *spec2 = (struct spec *) msgstr_descr; |
170 | 0 | bool err = false; |
171 | |
|
172 | 0 | if (spec1->named_arg_count + spec2->named_arg_count > 0) |
173 | 0 | { |
174 | 0 | size_t n1 = spec1->named_arg_count; |
175 | 0 | size_t n2 = spec2->named_arg_count; |
176 | | |
177 | | /* Check the argument names in spec1 are contained in those of spec2. |
178 | | Additional arguments in spec2 are allowed; they expand to themselves |
179 | | (including the surrounding braces) at runtime. |
180 | | Both arrays are sorted. We search for the differences. */ |
181 | 0 | size_t i, j; |
182 | 0 | for (i = 0, j = 0; i < n1 || j < n2; ) |
183 | 0 | { |
184 | 0 | int cmp = (i >= n1 ? 1 : |
185 | 0 | j >= n2 ? -1 : |
186 | 0 | strcmp (spec1->named[i].name, spec2->named[j].name)); |
187 | |
|
188 | 0 | if (cmp > 0) |
189 | 0 | j++; |
190 | 0 | else if (cmp < 0) |
191 | 0 | { |
192 | 0 | if (equality) |
193 | 0 | { |
194 | 0 | if (error_logger) |
195 | 0 | error_logger (error_logger_data, |
196 | 0 | _("a format specification for argument '%s' doesn't exist in '%s'"), |
197 | 0 | spec1->named[i].name, pretty_msgstr); |
198 | 0 | err = true; |
199 | 0 | break; |
200 | 0 | } |
201 | 0 | else |
202 | 0 | i++; |
203 | 0 | } |
204 | 0 | else |
205 | 0 | j++, i++; |
206 | 0 | } |
207 | 0 | } |
208 | |
|
209 | 0 | return err; |
210 | 0 | } |
211 | | |
212 | | |
213 | | struct formatstring_parser formatstring_perl_brace = |
214 | | { |
215 | | format_parse, |
216 | | format_free, |
217 | | format_get_number_of_directives, |
218 | | NULL, |
219 | | format_check |
220 | | }; |
221 | | |
222 | | |
223 | | #ifdef TEST |
224 | | |
225 | | /* Test program: Print the argument list specification returned by |
226 | | format_parse for strings read from standard input. */ |
227 | | |
228 | | #include <stdio.h> |
229 | | |
230 | | static void |
231 | | format_print (void *descr) |
232 | | { |
233 | | struct spec *spec = (struct spec *) descr; |
234 | | |
235 | | if (spec == NULL) |
236 | | { |
237 | | printf ("INVALID"); |
238 | | return; |
239 | | } |
240 | | |
241 | | printf ("{"); |
242 | | for (size_t i = 0; i < spec->named_arg_count; i++) |
243 | | { |
244 | | if (i > 0) |
245 | | printf (", "); |
246 | | printf ("'%s'", spec->named[i].name); |
247 | | } |
248 | | printf ("}"); |
249 | | } |
250 | | |
251 | | int |
252 | | main () |
253 | | { |
254 | | for (;;) |
255 | | { |
256 | | char *line = NULL; |
257 | | size_t line_size = 0; |
258 | | int line_len = getline (&line, &line_size, stdin); |
259 | | if (line_len < 0) |
260 | | break; |
261 | | if (line_len > 0 && line[line_len - 1] == '\n') |
262 | | line[--line_len] = '\0'; |
263 | | |
264 | | char *invalid_reason = NULL; |
265 | | void *descr = format_parse (line, false, NULL, &invalid_reason); |
266 | | |
267 | | format_print (descr); |
268 | | printf ("\n"); |
269 | | if (descr == NULL) |
270 | | printf ("%s\n", invalid_reason); |
271 | | |
272 | | free (invalid_reason); |
273 | | free (line); |
274 | | } |
275 | | |
276 | | return 0; |
277 | | } |
278 | | |
279 | | /* |
280 | | * For Emacs M-x compile |
281 | | * Local Variables: |
282 | | * 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-brace.c ../gnulib-lib/libgettextlib.la" |
283 | | * End: |
284 | | */ |
285 | | |
286 | | #endif /* TEST */ |