1*946379e7Schristos /* Perl brace format strings.
2*946379e7Schristos Copyright (C) 2004, 2006 Free Software Foundation, Inc.
3*946379e7Schristos Written by Bruno Haible <bruno@clisp.org>, 2003.
4*946379e7Schristos
5*946379e7Schristos This program is free software; you can redistribute it and/or modify
6*946379e7Schristos it under the terms of the GNU General Public License as published by
7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos any later version.
9*946379e7Schristos
10*946379e7Schristos This program is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*946379e7Schristos GNU General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU General Public License
16*946379e7Schristos along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*946379e7Schristos
19*946379e7Schristos #ifdef HAVE_CONFIG_H
20*946379e7Schristos # include <config.h>
21*946379e7Schristos #endif
22*946379e7Schristos
23*946379e7Schristos #include <stdbool.h>
24*946379e7Schristos #include <stdlib.h>
25*946379e7Schristos #include <string.h>
26*946379e7Schristos
27*946379e7Schristos #include "format.h"
28*946379e7Schristos #include "xalloc.h"
29*946379e7Schristos #include "gettext.h"
30*946379e7Schristos
31*946379e7Schristos #define _(str) gettext (str)
32*946379e7Schristos
33*946379e7Schristos /* Perl brace format strings are supported by Guido Flohr's libintl-perl
34*946379e7Schristos package, more precisely by the __expand and __x functions therein.
35*946379e7Schristos A format string directive here consists of
36*946379e7Schristos - an opening brace '{',
37*946379e7Schristos - an identifier [_A-Za-z][_0-9A-Za-z]*,
38*946379e7Schristos - a closing brace '}'.
39*946379e7Schristos */
40*946379e7Schristos
41*946379e7Schristos struct named_arg
42*946379e7Schristos {
43*946379e7Schristos char *name;
44*946379e7Schristos };
45*946379e7Schristos
46*946379e7Schristos struct spec
47*946379e7Schristos {
48*946379e7Schristos unsigned int directives;
49*946379e7Schristos unsigned int named_arg_count;
50*946379e7Schristos unsigned int allocated;
51*946379e7Schristos struct named_arg *named;
52*946379e7Schristos };
53*946379e7Schristos
54*946379e7Schristos
55*946379e7Schristos static int
named_arg_compare(const void * p1,const void * p2)56*946379e7Schristos named_arg_compare (const void *p1, const void *p2)
57*946379e7Schristos {
58*946379e7Schristos return strcmp (((const struct named_arg *) p1)->name,
59*946379e7Schristos ((const struct named_arg *) p2)->name);
60*946379e7Schristos }
61*946379e7Schristos
62*946379e7Schristos static void *
format_parse(const char * format,bool translated,char ** invalid_reason)63*946379e7Schristos format_parse (const char *format, bool translated, char **invalid_reason)
64*946379e7Schristos {
65*946379e7Schristos struct spec spec;
66*946379e7Schristos struct spec *result;
67*946379e7Schristos
68*946379e7Schristos spec.directives = 0;
69*946379e7Schristos spec.named_arg_count = 0;
70*946379e7Schristos spec.allocated = 0;
71*946379e7Schristos spec.named = NULL;
72*946379e7Schristos
73*946379e7Schristos for (; *format != '\0';)
74*946379e7Schristos if (*format++ == '{')
75*946379e7Schristos {
76*946379e7Schristos const char *f = format;
77*946379e7Schristos char c;
78*946379e7Schristos
79*946379e7Schristos c = *f;
80*946379e7Schristos if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
81*946379e7Schristos {
82*946379e7Schristos do
83*946379e7Schristos c = *++f;
84*946379e7Schristos while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'
85*946379e7Schristos || (c >= '0' && c <= '9'));
86*946379e7Schristos if (c == '}')
87*946379e7Schristos {
88*946379e7Schristos /* A directive. */
89*946379e7Schristos char *name;
90*946379e7Schristos const char *name_start = format;
91*946379e7Schristos const char *name_end = f;
92*946379e7Schristos size_t n = name_end - name_start;
93*946379e7Schristos
94*946379e7Schristos name = (char *) xmalloc (n + 1);
95*946379e7Schristos memcpy (name, name_start, n);
96*946379e7Schristos name[n] = '\0';
97*946379e7Schristos
98*946379e7Schristos spec.directives++;
99*946379e7Schristos
100*946379e7Schristos if (spec.allocated == spec.named_arg_count)
101*946379e7Schristos {
102*946379e7Schristos spec.allocated = 2 * spec.allocated + 1;
103*946379e7Schristos spec.named = (struct named_arg *) xrealloc (spec.named, spec.allocated * sizeof (struct named_arg));
104*946379e7Schristos }
105*946379e7Schristos spec.named[spec.named_arg_count].name = name;
106*946379e7Schristos spec.named_arg_count++;
107*946379e7Schristos
108*946379e7Schristos format = ++f;
109*946379e7Schristos }
110*946379e7Schristos }
111*946379e7Schristos }
112*946379e7Schristos
113*946379e7Schristos /* Sort the named argument array, and eliminate duplicates. */
114*946379e7Schristos if (spec.named_arg_count > 1)
115*946379e7Schristos {
116*946379e7Schristos unsigned int i, j;
117*946379e7Schristos
118*946379e7Schristos qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg),
119*946379e7Schristos named_arg_compare);
120*946379e7Schristos
121*946379e7Schristos /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
122*946379e7Schristos for (i = j = 0; i < spec.named_arg_count; i++)
123*946379e7Schristos if (j > 0 && strcmp (spec.named[i].name, spec.named[j-1].name) == 0)
124*946379e7Schristos free (spec.named[i].name);
125*946379e7Schristos else
126*946379e7Schristos {
127*946379e7Schristos if (j < i)
128*946379e7Schristos spec.named[j].name = spec.named[i].name;
129*946379e7Schristos j++;
130*946379e7Schristos }
131*946379e7Schristos spec.named_arg_count = j;
132*946379e7Schristos }
133*946379e7Schristos
134*946379e7Schristos result = (struct spec *) xmalloc (sizeof (struct spec));
135*946379e7Schristos *result = spec;
136*946379e7Schristos return result;
137*946379e7Schristos }
138*946379e7Schristos
139*946379e7Schristos static void
format_free(void * descr)140*946379e7Schristos format_free (void *descr)
141*946379e7Schristos {
142*946379e7Schristos struct spec *spec = (struct spec *) descr;
143*946379e7Schristos
144*946379e7Schristos if (spec->named != NULL)
145*946379e7Schristos {
146*946379e7Schristos unsigned int i;
147*946379e7Schristos for (i = 0; i < spec->named_arg_count; i++)
148*946379e7Schristos free (spec->named[i].name);
149*946379e7Schristos free (spec->named);
150*946379e7Schristos }
151*946379e7Schristos free (spec);
152*946379e7Schristos }
153*946379e7Schristos
154*946379e7Schristos static int
format_get_number_of_directives(void * descr)155*946379e7Schristos format_get_number_of_directives (void *descr)
156*946379e7Schristos {
157*946379e7Schristos struct spec *spec = (struct spec *) descr;
158*946379e7Schristos
159*946379e7Schristos return spec->directives;
160*946379e7Schristos }
161*946379e7Schristos
162*946379e7Schristos static bool
format_check(void * msgid_descr,void * msgstr_descr,bool equality,formatstring_error_logger_t error_logger,const char * pretty_msgstr)163*946379e7Schristos format_check (void *msgid_descr, void *msgstr_descr, bool equality,
164*946379e7Schristos formatstring_error_logger_t error_logger,
165*946379e7Schristos const char *pretty_msgstr)
166*946379e7Schristos {
167*946379e7Schristos struct spec *spec1 = (struct spec *) msgid_descr;
168*946379e7Schristos struct spec *spec2 = (struct spec *) msgstr_descr;
169*946379e7Schristos bool err = false;
170*946379e7Schristos
171*946379e7Schristos if (spec1->named_arg_count + spec2->named_arg_count > 0)
172*946379e7Schristos {
173*946379e7Schristos unsigned int i, j;
174*946379e7Schristos unsigned int n1 = spec1->named_arg_count;
175*946379e7Schristos unsigned int n2 = spec2->named_arg_count;
176*946379e7Schristos
177*946379e7Schristos /* Check the argument names in spec1 are contained in those of spec2.
178*946379e7Schristos Additional arguments in spec2 are allowed; they expand to themselves
179*946379e7Schristos (including the surrounding braces) at runtime.
180*946379e7Schristos Both arrays are sorted. We search for the differences. */
181*946379e7Schristos for (i = 0, j = 0; i < n1 || j < n2; )
182*946379e7Schristos {
183*946379e7Schristos int cmp = (i >= n1 ? 1 :
184*946379e7Schristos j >= n2 ? -1 :
185*946379e7Schristos strcmp (spec1->named[i].name, spec2->named[j].name));
186*946379e7Schristos
187*946379e7Schristos if (cmp > 0)
188*946379e7Schristos j++;
189*946379e7Schristos else if (cmp < 0)
190*946379e7Schristos {
191*946379e7Schristos if (equality)
192*946379e7Schristos {
193*946379e7Schristos if (error_logger)
194*946379e7Schristos error_logger (_("a format specification for argument '%s' doesn't exist in '%s'"),
195*946379e7Schristos spec1->named[i].name, pretty_msgstr);
196*946379e7Schristos err = true;
197*946379e7Schristos break;
198*946379e7Schristos }
199*946379e7Schristos else
200*946379e7Schristos i++;
201*946379e7Schristos }
202*946379e7Schristos else
203*946379e7Schristos j++, i++;
204*946379e7Schristos }
205*946379e7Schristos }
206*946379e7Schristos
207*946379e7Schristos return err;
208*946379e7Schristos }
209*946379e7Schristos
210*946379e7Schristos
211*946379e7Schristos struct formatstring_parser formatstring_perl_brace =
212*946379e7Schristos {
213*946379e7Schristos format_parse,
214*946379e7Schristos format_free,
215*946379e7Schristos format_get_number_of_directives,
216*946379e7Schristos NULL,
217*946379e7Schristos format_check
218*946379e7Schristos };
219*946379e7Schristos
220*946379e7Schristos
221*946379e7Schristos #ifdef TEST
222*946379e7Schristos
223*946379e7Schristos /* Test program: Print the argument list specification returned by
224*946379e7Schristos format_parse for strings read from standard input. */
225*946379e7Schristos
226*946379e7Schristos #include <stdio.h>
227*946379e7Schristos #include "getline.h"
228*946379e7Schristos
229*946379e7Schristos static void
format_print(void * descr)230*946379e7Schristos format_print (void *descr)
231*946379e7Schristos {
232*946379e7Schristos struct spec *spec = (struct spec *) descr;
233*946379e7Schristos unsigned int i;
234*946379e7Schristos
235*946379e7Schristos if (spec == NULL)
236*946379e7Schristos {
237*946379e7Schristos printf ("INVALID");
238*946379e7Schristos return;
239*946379e7Schristos }
240*946379e7Schristos
241*946379e7Schristos printf ("{");
242*946379e7Schristos for (i = 0; i < spec->named_arg_count; i++)
243*946379e7Schristos {
244*946379e7Schristos if (i > 0)
245*946379e7Schristos printf (", ");
246*946379e7Schristos printf ("'%s'", spec->named[i].name);
247*946379e7Schristos }
248*946379e7Schristos printf ("}");
249*946379e7Schristos }
250*946379e7Schristos
251*946379e7Schristos int
main()252*946379e7Schristos main ()
253*946379e7Schristos {
254*946379e7Schristos for (;;)
255*946379e7Schristos {
256*946379e7Schristos char *line = NULL;
257*946379e7Schristos size_t line_size = 0;
258*946379e7Schristos int line_len;
259*946379e7Schristos char *invalid_reason;
260*946379e7Schristos void *descr;
261*946379e7Schristos
262*946379e7Schristos line_len = getline (&line, &line_size, stdin);
263*946379e7Schristos if (line_len < 0)
264*946379e7Schristos break;
265*946379e7Schristos if (line_len > 0 && line[line_len - 1] == '\n')
266*946379e7Schristos line[--line_len] = '\0';
267*946379e7Schristos
268*946379e7Schristos invalid_reason = NULL;
269*946379e7Schristos descr = format_parse (line, false, &invalid_reason);
270*946379e7Schristos
271*946379e7Schristos format_print (descr);
272*946379e7Schristos printf ("\n");
273*946379e7Schristos if (descr == NULL)
274*946379e7Schristos printf ("%s\n", invalid_reason);
275*946379e7Schristos
276*946379e7Schristos free (invalid_reason);
277*946379e7Schristos free (line);
278*946379e7Schristos }
279*946379e7Schristos
280*946379e7Schristos return 0;
281*946379e7Schristos }
282*946379e7Schristos
283*946379e7Schristos /*
284*946379e7Schristos * For Emacs M-x compile
285*946379e7Schristos * Local Variables:
286*946379e7Schristos * compile-command: "/bin/sh ../libtool --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../lib -I../intl -DHAVE_CONFIG_H -DTEST format-perl-brace.c ../lib/libgettextlib.la"
287*946379e7Schristos * End:
288*946379e7Schristos */
289*946379e7Schristos
290*946379e7Schristos #endif /* TEST */
291