1*946379e7Schristos /* Python format strings.
2*946379e7Schristos Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
3*946379e7Schristos Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 "c-ctype.h"
29*946379e7Schristos #include "xalloc.h"
30*946379e7Schristos #include "xvasprintf.h"
31*946379e7Schristos #include "format-invalid.h"
32*946379e7Schristos #include "gettext.h"
33*946379e7Schristos
34*946379e7Schristos #define _(str) gettext (str)
35*946379e7Schristos
36*946379e7Schristos /* Python format strings are described in
37*946379e7Schristos Python Library reference
38*946379e7Schristos 2. Built-in Types, Exceptions and Functions
39*946379e7Schristos 2.1. Built-in Types
40*946379e7Schristos 2.1.5. Sequence Types
41*946379e7Schristos 2.1.5.2. String Formatting Operations
42*946379e7Schristos Any string or Unicode string can act as format string via the '%' operator,
43*946379e7Schristos implemented in stringobject.c and unicodeobject.c.
44*946379e7Schristos A directive
45*946379e7Schristos - starts with '%'
46*946379e7Schristos - is optionally followed by '(ident)' where ident is any sequence of
47*946379e7Schristos characters with balanced left and right parentheses,
48*946379e7Schristos - is optionally followed by any of the characters '-' (left justification),
49*946379e7Schristos '+' (sign), ' ' (blank), '#' (alt), '0' (zero), each of which acts as a
50*946379e7Schristos flag,
51*946379e7Schristos - is optionally followed by a width specification: '*' (reads an argument)
52*946379e7Schristos or a nonempty digit sequence,
53*946379e7Schristos - is optionally followed by '.' and a precision specification: '*' (reads
54*946379e7Schristos an argument) or a nonempty digit sequence,
55*946379e7Schristos - is optionally followed by a size specifier, one of 'h' 'l' 'L'.
56*946379e7Schristos - is finished by a specifier
57*946379e7Schristos - '%', that needs no argument,
58*946379e7Schristos - 'c', that needs a character argument,
59*946379e7Schristos - 's', 'r', that need a string argument,
60*946379e7Schristos - 'i', 'd', 'u', 'o', 'x', 'X', that need an integer argument,
61*946379e7Schristos - 'e', 'E', 'f', 'g', 'G', that need a floating-point argument.
62*946379e7Schristos Use of '(ident)' and use of unnamed argument specifications are exclusive,
63*946379e7Schristos because the first requires a mapping as argument, while the second requires
64*946379e7Schristos a tuple as argument.
65*946379e7Schristos */
66*946379e7Schristos
67*946379e7Schristos enum format_arg_type
68*946379e7Schristos {
69*946379e7Schristos FAT_NONE,
70*946379e7Schristos FAT_ANY,
71*946379e7Schristos FAT_CHARACTER,
72*946379e7Schristos FAT_STRING,
73*946379e7Schristos FAT_INTEGER,
74*946379e7Schristos FAT_FLOAT
75*946379e7Schristos };
76*946379e7Schristos
77*946379e7Schristos struct named_arg
78*946379e7Schristos {
79*946379e7Schristos char *name;
80*946379e7Schristos enum format_arg_type type;
81*946379e7Schristos };
82*946379e7Schristos
83*946379e7Schristos struct unnamed_arg
84*946379e7Schristos {
85*946379e7Schristos enum format_arg_type type;
86*946379e7Schristos };
87*946379e7Schristos
88*946379e7Schristos struct spec
89*946379e7Schristos {
90*946379e7Schristos unsigned int directives;
91*946379e7Schristos unsigned int named_arg_count;
92*946379e7Schristos unsigned int unnamed_arg_count;
93*946379e7Schristos unsigned int allocated;
94*946379e7Schristos struct named_arg *named;
95*946379e7Schristos struct unnamed_arg *unnamed;
96*946379e7Schristos };
97*946379e7Schristos
98*946379e7Schristos /* Locale independent test for a decimal digit.
99*946379e7Schristos Argument can be 'char' or 'unsigned char'. (Whereas the argument of
100*946379e7Schristos <ctype.h> isdigit must be an 'unsigned char'.) */
101*946379e7Schristos #undef isdigit
102*946379e7Schristos #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
103*946379e7Schristos
104*946379e7Schristos
105*946379e7Schristos static int
named_arg_compare(const void * p1,const void * p2)106*946379e7Schristos named_arg_compare (const void *p1, const void *p2)
107*946379e7Schristos {
108*946379e7Schristos return strcmp (((const struct named_arg *) p1)->name,
109*946379e7Schristos ((const struct named_arg *) p2)->name);
110*946379e7Schristos }
111*946379e7Schristos
112*946379e7Schristos #define INVALID_MIXES_NAMED_UNNAMED() \
113*946379e7Schristos xstrdup (_("The string refers to arguments both through argument names and through unnamed argument specifications."))
114*946379e7Schristos
115*946379e7Schristos static void *
format_parse(const char * format,bool translated,char ** invalid_reason)116*946379e7Schristos format_parse (const char *format, bool translated, char **invalid_reason)
117*946379e7Schristos {
118*946379e7Schristos struct spec spec;
119*946379e7Schristos struct spec *result;
120*946379e7Schristos
121*946379e7Schristos spec.directives = 0;
122*946379e7Schristos spec.named_arg_count = 0;
123*946379e7Schristos spec.unnamed_arg_count = 0;
124*946379e7Schristos spec.allocated = 0;
125*946379e7Schristos spec.named = NULL;
126*946379e7Schristos spec.unnamed = NULL;
127*946379e7Schristos
128*946379e7Schristos for (; *format != '\0';)
129*946379e7Schristos if (*format++ == '%')
130*946379e7Schristos {
131*946379e7Schristos /* A directive. */
132*946379e7Schristos char *name = NULL;
133*946379e7Schristos enum format_arg_type type;
134*946379e7Schristos
135*946379e7Schristos spec.directives++;
136*946379e7Schristos
137*946379e7Schristos if (*format == '(')
138*946379e7Schristos {
139*946379e7Schristos unsigned int depth;
140*946379e7Schristos const char *name_start;
141*946379e7Schristos const char *name_end;
142*946379e7Schristos size_t n;
143*946379e7Schristos
144*946379e7Schristos name_start = ++format;
145*946379e7Schristos depth = 0;
146*946379e7Schristos for (; *format != '\0'; format++)
147*946379e7Schristos {
148*946379e7Schristos if (*format == '(')
149*946379e7Schristos depth++;
150*946379e7Schristos else if (*format == ')')
151*946379e7Schristos {
152*946379e7Schristos if (depth == 0)
153*946379e7Schristos break;
154*946379e7Schristos else
155*946379e7Schristos depth--;
156*946379e7Schristos }
157*946379e7Schristos }
158*946379e7Schristos if (*format == '\0')
159*946379e7Schristos {
160*946379e7Schristos *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
161*946379e7Schristos goto bad_format;
162*946379e7Schristos }
163*946379e7Schristos name_end = format++;
164*946379e7Schristos
165*946379e7Schristos n = name_end - name_start;
166*946379e7Schristos name = (char *) xmalloc (n + 1);
167*946379e7Schristos memcpy (name, name_start, n);
168*946379e7Schristos name[n] = '\0';
169*946379e7Schristos }
170*946379e7Schristos
171*946379e7Schristos while (*format == '-' || *format == '+' || *format == ' '
172*946379e7Schristos || *format == '#' || *format == '0')
173*946379e7Schristos format++;
174*946379e7Schristos
175*946379e7Schristos if (*format == '*')
176*946379e7Schristos {
177*946379e7Schristos format++;
178*946379e7Schristos
179*946379e7Schristos /* Named and unnamed specifications are exclusive. */
180*946379e7Schristos if (spec.named_arg_count > 0)
181*946379e7Schristos {
182*946379e7Schristos *invalid_reason = INVALID_MIXES_NAMED_UNNAMED ();
183*946379e7Schristos goto bad_format;
184*946379e7Schristos }
185*946379e7Schristos
186*946379e7Schristos if (spec.allocated == spec.unnamed_arg_count)
187*946379e7Schristos {
188*946379e7Schristos spec.allocated = 2 * spec.allocated + 1;
189*946379e7Schristos spec.unnamed = (struct unnamed_arg *) xrealloc (spec.unnamed, spec.allocated * sizeof (struct unnamed_arg));
190*946379e7Schristos }
191*946379e7Schristos spec.unnamed[spec.unnamed_arg_count].type = FAT_INTEGER;
192*946379e7Schristos spec.unnamed_arg_count++;
193*946379e7Schristos }
194*946379e7Schristos else if (isdigit (*format))
195*946379e7Schristos {
196*946379e7Schristos do format++; while (isdigit (*format));
197*946379e7Schristos }
198*946379e7Schristos
199*946379e7Schristos if (*format == '.')
200*946379e7Schristos {
201*946379e7Schristos format++;
202*946379e7Schristos
203*946379e7Schristos if (*format == '*')
204*946379e7Schristos {
205*946379e7Schristos format++;
206*946379e7Schristos
207*946379e7Schristos /* Named and unnamed specifications are exclusive. */
208*946379e7Schristos if (spec.named_arg_count > 0)
209*946379e7Schristos {
210*946379e7Schristos *invalid_reason = INVALID_MIXES_NAMED_UNNAMED ();
211*946379e7Schristos goto bad_format;
212*946379e7Schristos }
213*946379e7Schristos
214*946379e7Schristos if (spec.allocated == spec.unnamed_arg_count)
215*946379e7Schristos {
216*946379e7Schristos spec.allocated = 2 * spec.allocated + 1;
217*946379e7Schristos spec.unnamed = (struct unnamed_arg *) xrealloc (spec.unnamed, spec.allocated * sizeof (struct unnamed_arg));
218*946379e7Schristos }
219*946379e7Schristos spec.unnamed[spec.unnamed_arg_count].type = FAT_INTEGER;
220*946379e7Schristos spec.unnamed_arg_count++;
221*946379e7Schristos }
222*946379e7Schristos else if (isdigit (*format))
223*946379e7Schristos {
224*946379e7Schristos do format++; while (isdigit (*format));
225*946379e7Schristos }
226*946379e7Schristos }
227*946379e7Schristos
228*946379e7Schristos if (*format == 'h' || *format == 'l' || *format == 'L')
229*946379e7Schristos format++;
230*946379e7Schristos
231*946379e7Schristos switch (*format)
232*946379e7Schristos {
233*946379e7Schristos case '%':
234*946379e7Schristos type = FAT_ANY;
235*946379e7Schristos break;
236*946379e7Schristos case 'c':
237*946379e7Schristos type = FAT_CHARACTER;
238*946379e7Schristos break;
239*946379e7Schristos case 's': case 'r':
240*946379e7Schristos type = FAT_STRING;
241*946379e7Schristos break;
242*946379e7Schristos case 'i': case 'd': case 'u': case 'o': case 'x': case 'X':
243*946379e7Schristos type = FAT_INTEGER;
244*946379e7Schristos break;
245*946379e7Schristos case 'e': case 'E': case 'f': case 'g': case 'G':
246*946379e7Schristos type = FAT_FLOAT;
247*946379e7Schristos break;
248*946379e7Schristos default:
249*946379e7Schristos *invalid_reason =
250*946379e7Schristos (*format == '\0'
251*946379e7Schristos ? INVALID_UNTERMINATED_DIRECTIVE ()
252*946379e7Schristos : INVALID_CONVERSION_SPECIFIER (spec.directives, *format));
253*946379e7Schristos goto bad_format;
254*946379e7Schristos }
255*946379e7Schristos
256*946379e7Schristos if (name != NULL)
257*946379e7Schristos {
258*946379e7Schristos /* Named argument. */
259*946379e7Schristos
260*946379e7Schristos /* Named and unnamed specifications are exclusive. */
261*946379e7Schristos if (spec.unnamed_arg_count > 0)
262*946379e7Schristos {
263*946379e7Schristos *invalid_reason = INVALID_MIXES_NAMED_UNNAMED ();
264*946379e7Schristos goto bad_format;
265*946379e7Schristos }
266*946379e7Schristos
267*946379e7Schristos if (spec.allocated == spec.named_arg_count)
268*946379e7Schristos {
269*946379e7Schristos spec.allocated = 2 * spec.allocated + 1;
270*946379e7Schristos spec.named = (struct named_arg *) xrealloc (spec.named, spec.allocated * sizeof (struct named_arg));
271*946379e7Schristos }
272*946379e7Schristos spec.named[spec.named_arg_count].name = name;
273*946379e7Schristos spec.named[spec.named_arg_count].type = type;
274*946379e7Schristos spec.named_arg_count++;
275*946379e7Schristos }
276*946379e7Schristos else if (*format != '%')
277*946379e7Schristos {
278*946379e7Schristos /* Unnamed argument. */
279*946379e7Schristos
280*946379e7Schristos /* Named and unnamed specifications are exclusive. */
281*946379e7Schristos if (spec.named_arg_count > 0)
282*946379e7Schristos {
283*946379e7Schristos *invalid_reason = INVALID_MIXES_NAMED_UNNAMED ();
284*946379e7Schristos goto bad_format;
285*946379e7Schristos }
286*946379e7Schristos
287*946379e7Schristos if (spec.allocated == spec.unnamed_arg_count)
288*946379e7Schristos {
289*946379e7Schristos spec.allocated = 2 * spec.allocated + 1;
290*946379e7Schristos spec.unnamed = (struct unnamed_arg *) xrealloc (spec.unnamed, spec.allocated * sizeof (struct unnamed_arg));
291*946379e7Schristos }
292*946379e7Schristos spec.unnamed[spec.unnamed_arg_count].type = type;
293*946379e7Schristos spec.unnamed_arg_count++;
294*946379e7Schristos }
295*946379e7Schristos
296*946379e7Schristos format++;
297*946379e7Schristos }
298*946379e7Schristos
299*946379e7Schristos /* Sort the named argument array, and eliminate duplicates. */
300*946379e7Schristos if (spec.named_arg_count > 1)
301*946379e7Schristos {
302*946379e7Schristos unsigned int i, j;
303*946379e7Schristos bool err;
304*946379e7Schristos
305*946379e7Schristos qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg),
306*946379e7Schristos named_arg_compare);
307*946379e7Schristos
308*946379e7Schristos /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
309*946379e7Schristos err = false;
310*946379e7Schristos for (i = j = 0; i < spec.named_arg_count; i++)
311*946379e7Schristos if (j > 0 && strcmp (spec.named[i].name, spec.named[j-1].name) == 0)
312*946379e7Schristos {
313*946379e7Schristos enum format_arg_type type1 = spec.named[i].type;
314*946379e7Schristos enum format_arg_type type2 = spec.named[j-1].type;
315*946379e7Schristos enum format_arg_type type_both;
316*946379e7Schristos
317*946379e7Schristos if (type1 == type2 || type2 == FAT_ANY)
318*946379e7Schristos type_both = type1;
319*946379e7Schristos else if (type1 == FAT_ANY)
320*946379e7Schristos type_both = type2;
321*946379e7Schristos else
322*946379e7Schristos {
323*946379e7Schristos /* Incompatible types. */
324*946379e7Schristos type_both = FAT_NONE;
325*946379e7Schristos if (!err)
326*946379e7Schristos *invalid_reason =
327*946379e7Schristos xasprintf (_("The string refers to the argument named '%s' in incompatible ways."), spec.named[i].name);
328*946379e7Schristos err = true;
329*946379e7Schristos }
330*946379e7Schristos
331*946379e7Schristos spec.named[j-1].type = type_both;
332*946379e7Schristos free (spec.named[i].name);
333*946379e7Schristos }
334*946379e7Schristos else
335*946379e7Schristos {
336*946379e7Schristos if (j < i)
337*946379e7Schristos {
338*946379e7Schristos spec.named[j].name = spec.named[i].name;
339*946379e7Schristos spec.named[j].type = spec.named[i].type;
340*946379e7Schristos }
341*946379e7Schristos j++;
342*946379e7Schristos }
343*946379e7Schristos spec.named_arg_count = j;
344*946379e7Schristos if (err)
345*946379e7Schristos /* *invalid_reason has already been set above. */
346*946379e7Schristos goto bad_format;
347*946379e7Schristos }
348*946379e7Schristos
349*946379e7Schristos result = (struct spec *) xmalloc (sizeof (struct spec));
350*946379e7Schristos *result = spec;
351*946379e7Schristos return result;
352*946379e7Schristos
353*946379e7Schristos bad_format:
354*946379e7Schristos if (spec.named != NULL)
355*946379e7Schristos {
356*946379e7Schristos unsigned int i;
357*946379e7Schristos for (i = 0; i < spec.named_arg_count; i++)
358*946379e7Schristos free (spec.named[i].name);
359*946379e7Schristos free (spec.named);
360*946379e7Schristos }
361*946379e7Schristos if (spec.unnamed != NULL)
362*946379e7Schristos free (spec.unnamed);
363*946379e7Schristos return NULL;
364*946379e7Schristos }
365*946379e7Schristos
366*946379e7Schristos static void
format_free(void * descr)367*946379e7Schristos format_free (void *descr)
368*946379e7Schristos {
369*946379e7Schristos struct spec *spec = (struct spec *) descr;
370*946379e7Schristos
371*946379e7Schristos if (spec->named != NULL)
372*946379e7Schristos {
373*946379e7Schristos unsigned int i;
374*946379e7Schristos for (i = 0; i < spec->named_arg_count; i++)
375*946379e7Schristos free (spec->named[i].name);
376*946379e7Schristos free (spec->named);
377*946379e7Schristos }
378*946379e7Schristos if (spec->unnamed != NULL)
379*946379e7Schristos free (spec->unnamed);
380*946379e7Schristos free (spec);
381*946379e7Schristos }
382*946379e7Schristos
383*946379e7Schristos static int
format_get_number_of_directives(void * descr)384*946379e7Schristos format_get_number_of_directives (void *descr)
385*946379e7Schristos {
386*946379e7Schristos struct spec *spec = (struct spec *) descr;
387*946379e7Schristos
388*946379e7Schristos return spec->directives;
389*946379e7Schristos }
390*946379e7Schristos
391*946379e7Schristos static bool
format_check(void * msgid_descr,void * msgstr_descr,bool equality,formatstring_error_logger_t error_logger,const char * pretty_msgstr)392*946379e7Schristos format_check (void *msgid_descr, void *msgstr_descr, bool equality,
393*946379e7Schristos formatstring_error_logger_t error_logger,
394*946379e7Schristos const char *pretty_msgstr)
395*946379e7Schristos {
396*946379e7Schristos struct spec *spec1 = (struct spec *) msgid_descr;
397*946379e7Schristos struct spec *spec2 = (struct spec *) msgstr_descr;
398*946379e7Schristos bool err = false;
399*946379e7Schristos
400*946379e7Schristos if (spec1->named_arg_count > 0 && spec2->unnamed_arg_count > 0)
401*946379e7Schristos {
402*946379e7Schristos if (error_logger)
403*946379e7Schristos error_logger (_("format specifications in 'msgid' expect a mapping, those in '%s' expect a tuple"),
404*946379e7Schristos pretty_msgstr);
405*946379e7Schristos err = true;
406*946379e7Schristos }
407*946379e7Schristos else if (spec1->unnamed_arg_count > 0 && spec2->named_arg_count > 0)
408*946379e7Schristos {
409*946379e7Schristos if (error_logger)
410*946379e7Schristos error_logger (_("format specifications in 'msgid' expect a tuple, those in '%s' expect a mapping"),
411*946379e7Schristos pretty_msgstr);
412*946379e7Schristos err = true;
413*946379e7Schristos }
414*946379e7Schristos else
415*946379e7Schristos {
416*946379e7Schristos if (spec1->named_arg_count + spec2->named_arg_count > 0)
417*946379e7Schristos {
418*946379e7Schristos unsigned int i, j;
419*946379e7Schristos unsigned int n1 = spec1->named_arg_count;
420*946379e7Schristos unsigned int n2 = spec2->named_arg_count;
421*946379e7Schristos
422*946379e7Schristos /* Check the argument names are the same.
423*946379e7Schristos Both arrays are sorted. We search for the first difference. */
424*946379e7Schristos for (i = 0, j = 0; i < n1 || j < n2; )
425*946379e7Schristos {
426*946379e7Schristos int cmp = (i >= n1 ? 1 :
427*946379e7Schristos j >= n2 ? -1 :
428*946379e7Schristos strcmp (spec1->named[i].name, spec2->named[j].name));
429*946379e7Schristos
430*946379e7Schristos if (cmp > 0)
431*946379e7Schristos {
432*946379e7Schristos if (error_logger)
433*946379e7Schristos error_logger (_("a format specification for argument '%s', as in '%s', doesn't exist in 'msgid'"),
434*946379e7Schristos spec2->named[j].name, pretty_msgstr);
435*946379e7Schristos err = true;
436*946379e7Schristos break;
437*946379e7Schristos }
438*946379e7Schristos else if (cmp < 0)
439*946379e7Schristos {
440*946379e7Schristos if (equality)
441*946379e7Schristos {
442*946379e7Schristos if (error_logger)
443*946379e7Schristos error_logger (_("a format specification for argument '%s' doesn't exist in '%s'"),
444*946379e7Schristos spec1->named[i].name, pretty_msgstr);
445*946379e7Schristos err = true;
446*946379e7Schristos break;
447*946379e7Schristos }
448*946379e7Schristos else
449*946379e7Schristos i++;
450*946379e7Schristos }
451*946379e7Schristos else
452*946379e7Schristos j++, i++;
453*946379e7Schristos }
454*946379e7Schristos /* Check the argument types are the same. */
455*946379e7Schristos if (!err)
456*946379e7Schristos for (i = 0, j = 0; j < n2; )
457*946379e7Schristos {
458*946379e7Schristos if (strcmp (spec1->named[i].name, spec2->named[j].name) == 0)
459*946379e7Schristos {
460*946379e7Schristos if (spec1->named[i].type != spec2->named[j].type)
461*946379e7Schristos {
462*946379e7Schristos if (error_logger)
463*946379e7Schristos error_logger (_("format specifications in 'msgid' and '%s' for argument '%s' are not the same"),
464*946379e7Schristos pretty_msgstr, spec2->named[j].name);
465*946379e7Schristos err = true;
466*946379e7Schristos break;
467*946379e7Schristos }
468*946379e7Schristos j++, i++;
469*946379e7Schristos }
470*946379e7Schristos else
471*946379e7Schristos i++;
472*946379e7Schristos }
473*946379e7Schristos }
474*946379e7Schristos
475*946379e7Schristos if (spec1->unnamed_arg_count + spec2->unnamed_arg_count > 0)
476*946379e7Schristos {
477*946379e7Schristos unsigned int i;
478*946379e7Schristos
479*946379e7Schristos /* Check the argument types are the same. */
480*946379e7Schristos if (equality
481*946379e7Schristos ? spec1->unnamed_arg_count != spec2->unnamed_arg_count
482*946379e7Schristos : spec1->unnamed_arg_count < spec2->unnamed_arg_count)
483*946379e7Schristos {
484*946379e7Schristos if (error_logger)
485*946379e7Schristos error_logger (_("number of format specifications in 'msgid' and '%s' does not match"),
486*946379e7Schristos pretty_msgstr);
487*946379e7Schristos err = true;
488*946379e7Schristos }
489*946379e7Schristos else
490*946379e7Schristos for (i = 0; i < spec2->unnamed_arg_count; i++)
491*946379e7Schristos if (spec1->unnamed[i].type != spec2->unnamed[i].type)
492*946379e7Schristos {
493*946379e7Schristos if (error_logger)
494*946379e7Schristos error_logger (_("format specifications in 'msgid' and '%s' for argument %u are not the same"),
495*946379e7Schristos pretty_msgstr, i + 1);
496*946379e7Schristos err = true;
497*946379e7Schristos }
498*946379e7Schristos }
499*946379e7Schristos }
500*946379e7Schristos
501*946379e7Schristos return err;
502*946379e7Schristos }
503*946379e7Schristos
504*946379e7Schristos
505*946379e7Schristos struct formatstring_parser formatstring_python =
506*946379e7Schristos {
507*946379e7Schristos format_parse,
508*946379e7Schristos format_free,
509*946379e7Schristos format_get_number_of_directives,
510*946379e7Schristos NULL,
511*946379e7Schristos format_check
512*946379e7Schristos };
513*946379e7Schristos
514*946379e7Schristos
515*946379e7Schristos unsigned int
get_python_format_unnamed_arg_count(const char * string)516*946379e7Schristos get_python_format_unnamed_arg_count (const char *string)
517*946379e7Schristos {
518*946379e7Schristos /* Parse the format string. */
519*946379e7Schristos char *invalid_reason = NULL;
520*946379e7Schristos struct spec *descr =
521*946379e7Schristos (struct spec *) format_parse (string, false, &invalid_reason);
522*946379e7Schristos
523*946379e7Schristos if (descr != NULL)
524*946379e7Schristos {
525*946379e7Schristos unsigned int result = descr->unnamed_arg_count;
526*946379e7Schristos
527*946379e7Schristos format_free (descr);
528*946379e7Schristos return result;
529*946379e7Schristos }
530*946379e7Schristos else
531*946379e7Schristos {
532*946379e7Schristos free (invalid_reason);
533*946379e7Schristos return 0;
534*946379e7Schristos }
535*946379e7Schristos }
536*946379e7Schristos
537*946379e7Schristos
538*946379e7Schristos #ifdef TEST
539*946379e7Schristos
540*946379e7Schristos /* Test program: Print the argument list specification returned by
541*946379e7Schristos format_parse for strings read from standard input. */
542*946379e7Schristos
543*946379e7Schristos #include <stdio.h>
544*946379e7Schristos #include "getline.h"
545*946379e7Schristos
546*946379e7Schristos static void
format_print(void * descr)547*946379e7Schristos format_print (void *descr)
548*946379e7Schristos {
549*946379e7Schristos struct spec *spec = (struct spec *) descr;
550*946379e7Schristos unsigned int i;
551*946379e7Schristos
552*946379e7Schristos if (spec == NULL)
553*946379e7Schristos {
554*946379e7Schristos printf ("INVALID");
555*946379e7Schristos return;
556*946379e7Schristos }
557*946379e7Schristos
558*946379e7Schristos if (spec->named_arg_count > 0)
559*946379e7Schristos {
560*946379e7Schristos if (spec->unnamed_arg_count > 0)
561*946379e7Schristos abort ();
562*946379e7Schristos
563*946379e7Schristos printf ("{");
564*946379e7Schristos for (i = 0; i < spec->named_arg_count; i++)
565*946379e7Schristos {
566*946379e7Schristos if (i > 0)
567*946379e7Schristos printf (", ");
568*946379e7Schristos printf ("'%s':", spec->named[i].name);
569*946379e7Schristos switch (spec->named[i].type)
570*946379e7Schristos {
571*946379e7Schristos case FAT_ANY:
572*946379e7Schristos printf ("*");
573*946379e7Schristos break;
574*946379e7Schristos case FAT_CHARACTER:
575*946379e7Schristos printf ("c");
576*946379e7Schristos break;
577*946379e7Schristos case FAT_STRING:
578*946379e7Schristos printf ("s");
579*946379e7Schristos break;
580*946379e7Schristos case FAT_INTEGER:
581*946379e7Schristos printf ("i");
582*946379e7Schristos break;
583*946379e7Schristos case FAT_FLOAT:
584*946379e7Schristos printf ("f");
585*946379e7Schristos break;
586*946379e7Schristos default:
587*946379e7Schristos abort ();
588*946379e7Schristos }
589*946379e7Schristos }
590*946379e7Schristos printf ("}");
591*946379e7Schristos }
592*946379e7Schristos else
593*946379e7Schristos {
594*946379e7Schristos printf ("(");
595*946379e7Schristos for (i = 0; i < spec->unnamed_arg_count; i++)
596*946379e7Schristos {
597*946379e7Schristos if (i > 0)
598*946379e7Schristos printf (" ");
599*946379e7Schristos switch (spec->unnamed[i].type)
600*946379e7Schristos {
601*946379e7Schristos case FAT_ANY:
602*946379e7Schristos printf ("*");
603*946379e7Schristos break;
604*946379e7Schristos case FAT_CHARACTER:
605*946379e7Schristos printf ("c");
606*946379e7Schristos break;
607*946379e7Schristos case FAT_STRING:
608*946379e7Schristos printf ("s");
609*946379e7Schristos break;
610*946379e7Schristos case FAT_INTEGER:
611*946379e7Schristos printf ("i");
612*946379e7Schristos break;
613*946379e7Schristos case FAT_FLOAT:
614*946379e7Schristos printf ("f");
615*946379e7Schristos break;
616*946379e7Schristos default:
617*946379e7Schristos abort ();
618*946379e7Schristos }
619*946379e7Schristos }
620*946379e7Schristos printf (")");
621*946379e7Schristos }
622*946379e7Schristos }
623*946379e7Schristos
624*946379e7Schristos int
main()625*946379e7Schristos main ()
626*946379e7Schristos {
627*946379e7Schristos for (;;)
628*946379e7Schristos {
629*946379e7Schristos char *line = NULL;
630*946379e7Schristos size_t line_size = 0;
631*946379e7Schristos int line_len;
632*946379e7Schristos char *invalid_reason;
633*946379e7Schristos void *descr;
634*946379e7Schristos
635*946379e7Schristos line_len = getline (&line, &line_size, stdin);
636*946379e7Schristos if (line_len < 0)
637*946379e7Schristos break;
638*946379e7Schristos if (line_len > 0 && line[line_len - 1] == '\n')
639*946379e7Schristos line[--line_len] = '\0';
640*946379e7Schristos
641*946379e7Schristos invalid_reason = NULL;
642*946379e7Schristos descr = format_parse (line, false, &invalid_reason);
643*946379e7Schristos
644*946379e7Schristos format_print (descr);
645*946379e7Schristos printf ("\n");
646*946379e7Schristos if (descr == NULL)
647*946379e7Schristos printf ("%s\n", invalid_reason);
648*946379e7Schristos
649*946379e7Schristos free (invalid_reason);
650*946379e7Schristos free (line);
651*946379e7Schristos }
652*946379e7Schristos
653*946379e7Schristos return 0;
654*946379e7Schristos }
655*946379e7Schristos
656*946379e7Schristos /*
657*946379e7Schristos * For Emacs M-x compile
658*946379e7Schristos * Local Variables:
659*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-python.c ../lib/libgettextlib.la"
660*946379e7Schristos * End:
661*946379e7Schristos */
662*946379e7Schristos
663*946379e7Schristos #endif /* TEST */
664