No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / src / format-tcl.c
blob08b78a68671b14d83f74f1050dbf7385fb79cbb4
1 /* Tcl format strings.
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2002.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <stdbool.h>
24 #include <stdlib.h>
26 #include "format.h"
27 #include "c-ctype.h"
28 #include "xalloc.h"
29 #include "xerror.h"
30 #include "format-invalid.h"
31 #include "gettext.h"
33 #define _(str) gettext (str)
35 /* Tcl format strings are described in the tcl8.3.3/doc/format.n manual
36 page and implemented in the function Tcl_FormatObjCmd in
37 tcl8.3.3/generic/tclCmdAH.c.
38 A directive
39 - starts with '%' or '%m$' where m is a positive integer,
40 - is optionally followed by any of the characters '#', '0', '-', ' ', '+',
41 each of which acts as a flag,
42 - is optionally followed by a width specification: '*' (reads an argument)
43 or a nonempty digit sequence,
44 - is optionally followed by '.' and a precision specification: '*' (reads
45 an argument) or a nonempty digit sequence,
46 - is optionally followed by a size specifier, 'h' or 'l'. 'l' is ignored.
47 - is finished by a specifier
48 - '%', that needs no argument,
49 - 'c', that needs a character argument,
50 - 's', that needs a string argument,
51 - 'i', 'd', that need a signed integer argument,
52 - 'o', 'u', 'x', 'X', that need an unsigned integer argument,
53 - 'e', 'E', 'f', 'g', 'G', that need a floating-point argument.
54 Numbered ('%m$') and unnumbered argument specifications cannot be used
55 in the same string.
58 enum format_arg_type
60 FAT_NONE,
61 FAT_CHARACTER,
62 FAT_STRING,
63 FAT_INTEGER,
64 FAT_UNSIGNED_INTEGER,
65 FAT_SHORT_INTEGER,
66 FAT_SHORT_UNSIGNED_INTEGER,
67 FAT_FLOAT
70 struct numbered_arg
72 unsigned int number;
73 enum format_arg_type type;
76 struct spec
78 unsigned int directives;
79 unsigned int numbered_arg_count;
80 unsigned int allocated;
81 struct numbered_arg *numbered;
84 /* Locale independent test for a decimal digit.
85 Argument can be 'char' or 'unsigned char'. (Whereas the argument of
86 <ctype.h> isdigit must be an 'unsigned char'.) */
87 #undef isdigit
88 #define isdigit(c) ((unsigned int) ((c) - '0') < 10)
91 static int
92 numbered_arg_compare (const void *p1, const void *p2)
94 unsigned int n1 = ((const struct numbered_arg *) p1)->number;
95 unsigned int n2 = ((const struct numbered_arg *) p2)->number;
97 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
100 static void *
101 format_parse (const char *format, bool translated, char **invalid_reason)
103 struct spec spec;
104 struct spec *result;
105 bool seen_numbered_arg;
106 bool seen_unnumbered_arg;
107 unsigned int number;
109 spec.directives = 0;
110 spec.numbered_arg_count = 0;
111 spec.allocated = 0;
112 spec.numbered = NULL;
113 seen_numbered_arg = false;
114 seen_unnumbered_arg = false;
115 number = 1;
117 for (; *format != '\0';)
118 if (*format++ == '%')
120 /* A directive. */
121 spec.directives++;
123 if (*format != '%')
125 bool is_numbered_arg;
126 bool short_flag;
127 enum format_arg_type type;
129 is_numbered_arg = false;
130 if (isdigit (*format))
132 const char *f = format;
133 unsigned int m = 0;
137 m = 10 * m + (*f - '0');
138 f++;
140 while (isdigit (*f));
142 if (*f == '$')
144 if (m == 0)
146 *invalid_reason = INVALID_ARGNO_0 (spec.directives);
147 goto bad_format;
149 number = m;
150 format = ++f;
152 /* Numbered and unnumbered specifications are exclusive. */
153 if (seen_unnumbered_arg)
155 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
156 goto bad_format;
158 is_numbered_arg = true;
159 seen_numbered_arg = true;
163 /* Numbered and unnumbered specifications are exclusive. */
164 if (!is_numbered_arg)
166 if (seen_numbered_arg)
168 *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
169 goto bad_format;
171 seen_unnumbered_arg = true;
174 /* Parse flags. */
175 while (*format == ' ' || *format == '+' || *format == '-'
176 || *format == '#' || *format == '0')
177 format++;
179 /* Parse width. */
180 if (*format == '*')
182 format++;
184 if (spec.allocated == spec.numbered_arg_count)
186 spec.allocated = 2 * spec.allocated + 1;
187 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
189 spec.numbered[spec.numbered_arg_count].number = number;
190 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
191 spec.numbered_arg_count++;
193 number++;
195 else if (isdigit (*format))
197 do format++; while (isdigit (*format));
200 /* Parse precision. */
201 if (*format == '.')
203 format++;
205 if (*format == '*')
207 format++;
209 if (spec.allocated == spec.numbered_arg_count)
211 spec.allocated = 2 * spec.allocated + 1;
212 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
214 spec.numbered[spec.numbered_arg_count].number = number;
215 spec.numbered[spec.numbered_arg_count].type = FAT_INTEGER;
216 spec.numbered_arg_count++;
218 number++;
220 else if (isdigit (*format))
222 do format++; while (isdigit (*format));
226 /* Parse optional size specification. */
227 short_flag = false;
228 if (*format == 'h')
229 short_flag = true, format++;
230 else if (*format == 'l')
231 format++;
233 switch (*format)
235 case 'c':
236 type = FAT_CHARACTER;
237 break;
238 case 's':
239 type = FAT_STRING;
240 break;
241 case 'i': case 'd':
242 type = (short_flag ? FAT_SHORT_INTEGER : FAT_INTEGER);
243 break;
244 case 'u': case 'o': case 'x': case 'X':
245 type = (short_flag ? FAT_SHORT_UNSIGNED_INTEGER : FAT_UNSIGNED_INTEGER);
246 break;
247 case 'e': case 'E': case 'f': case 'g': case 'G':
248 type = FAT_FLOAT;
249 break;
250 default:
251 *invalid_reason =
252 (*format == '\0'
253 ? INVALID_UNTERMINATED_DIRECTIVE ()
254 : INVALID_CONVERSION_SPECIFIER (spec.directives, *format));
255 goto bad_format;
258 if (spec.allocated == spec.numbered_arg_count)
260 spec.allocated = 2 * spec.allocated + 1;
261 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
263 spec.numbered[spec.numbered_arg_count].number = number;
264 spec.numbered[spec.numbered_arg_count].type = type;
265 spec.numbered_arg_count++;
267 number++;
270 format++;
273 /* Sort the numbered argument array, and eliminate duplicates. */
274 if (spec.numbered_arg_count > 1)
276 unsigned int i, j;
277 bool err;
279 qsort (spec.numbered, spec.numbered_arg_count,
280 sizeof (struct numbered_arg), numbered_arg_compare);
282 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
283 err = false;
284 for (i = j = 0; i < spec.numbered_arg_count; i++)
285 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
287 enum format_arg_type type1 = spec.numbered[i].type;
288 enum format_arg_type type2 = spec.numbered[j-1].type;
289 enum format_arg_type type_both;
291 if (type1 == type2)
292 type_both = type1;
293 else
295 /* Incompatible types. */
296 type_both = FAT_NONE;
297 if (!err)
298 *invalid_reason =
299 INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
300 err = true;
303 spec.numbered[j-1].type = type_both;
305 else
307 if (j < i)
309 spec.numbered[j].number = spec.numbered[i].number;
310 spec.numbered[j].type = spec.numbered[i].type;
312 j++;
314 spec.numbered_arg_count = j;
315 if (err)
316 /* *invalid_reason has already been set above. */
317 goto bad_format;
320 result = (struct spec *) xmalloc (sizeof (struct spec));
321 *result = spec;
322 return result;
324 bad_format:
325 if (spec.numbered != NULL)
326 free (spec.numbered);
327 return NULL;
330 static void
331 format_free (void *descr)
333 struct spec *spec = (struct spec *) descr;
335 if (spec->numbered != NULL)
336 free (spec->numbered);
337 free (spec);
340 static int
341 format_get_number_of_directives (void *descr)
343 struct spec *spec = (struct spec *) descr;
345 return spec->directives;
348 static bool
349 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
350 formatstring_error_logger_t error_logger,
351 const char *pretty_msgstr)
353 struct spec *spec1 = (struct spec *) msgid_descr;
354 struct spec *spec2 = (struct spec *) msgstr_descr;
355 bool err = false;
357 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
359 unsigned int i, j;
360 unsigned int n1 = spec1->numbered_arg_count;
361 unsigned int n2 = spec2->numbered_arg_count;
363 /* Check the argument names are the same.
364 Both arrays are sorted. We search for the first difference. */
365 for (i = 0, j = 0; i < n1 || j < n2; )
367 int cmp = (i >= n1 ? 1 :
368 j >= n2 ? -1 :
369 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
370 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
373 if (cmp > 0)
375 if (error_logger)
376 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
377 spec2->numbered[j].number, pretty_msgstr);
378 err = true;
379 break;
381 else if (cmp < 0)
383 if (equality)
385 if (error_logger)
386 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
387 spec1->numbered[i].number, pretty_msgstr);
388 err = true;
389 break;
391 else
392 i++;
394 else
395 j++, i++;
397 /* Check the argument types are the same. */
398 if (!err)
399 for (i = 0, j = 0; j < n2; )
401 if (spec1->numbered[i].number == spec2->numbered[j].number)
403 if (spec1->numbered[i].type != spec2->numbered[j].type)
405 if (error_logger)
406 error_logger (_("format specifications in 'msgid' and '%s' for argument %u are not the same"),
407 pretty_msgstr, spec2->numbered[j].number);
408 err = true;
409 break;
411 j++, i++;
413 else
414 i++;
418 return err;
422 struct formatstring_parser formatstring_tcl =
424 format_parse,
425 format_free,
426 format_get_number_of_directives,
427 format_check
431 #ifdef TEST
433 /* Test program: Print the argument list specification returned by
434 format_parse for strings read from standard input. */
436 #include <stdio.h>
437 #include "getline.h"
439 static void
440 format_print (void *descr)
442 struct spec *spec = (struct spec *) descr;
443 unsigned int last;
444 unsigned int i;
446 if (spec == NULL)
448 printf ("INVALID");
449 return;
452 printf ("(");
453 last = 1;
454 for (i = 0; i < spec->numbered_arg_count; i++)
456 unsigned int number = spec->numbered[i].number;
458 if (i > 0)
459 printf (" ");
460 if (number < last)
461 abort ();
462 for (; last < number; last++)
463 printf ("_ ");
464 switch (spec->numbered[i].type)
466 case FAT_CHARACTER:
467 printf ("c");
468 break;
469 case FAT_STRING:
470 printf ("s");
471 break;
472 case FAT_INTEGER:
473 printf ("i");
474 break;
475 case FAT_UNSIGNED_INTEGER:
476 printf ("[unsigned]i");
477 break;
478 case FAT_SHORT_INTEGER:
479 printf ("hi");
480 break;
481 case FAT_SHORT_UNSIGNED_INTEGER:
482 printf ("[unsigned]hi");
483 break;
484 case FAT_FLOAT:
485 printf ("f");
486 break;
487 default:
488 abort ();
490 last = number + 1;
492 printf (")");
496 main ()
498 for (;;)
500 char *line = NULL;
501 size_t line_size = 0;
502 int line_len;
503 char *invalid_reason;
504 void *descr;
506 line_len = getline (&line, &line_size, stdin);
507 if (line_len < 0)
508 break;
509 if (line_len > 0 && line[line_len - 1] == '\n')
510 line[--line_len] = '\0';
512 invalid_reason = NULL;
513 descr = format_parse (line, false, &invalid_reason);
515 format_print (descr);
516 printf ("\n");
517 if (descr == NULL)
518 printf ("%s\n", invalid_reason);
520 free (invalid_reason);
521 free (line);
524 return 0;
528 * For Emacs M-x compile
529 * Local Variables:
530 * 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-tcl.c ../lib/libgettextlib.la"
531 * End:
534 #endif /* TEST */