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