No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / src / format-ycp.c
blob803c973731cc498821fba753191b89b0ca8a7c28
1 /* YCP and Smalltalk format strings.
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 /* YCP sformat strings are described in libycp documentation YCP-builtins.html.
36 A directive starts with '%' and is followed by '%' or a nonzero digit ('1'
37 to '9').
38 GNU Smalltalk format strings are described in the CharArray documentation,
39 methods 'bindWith:' and 'bindWithArguments:'. They have the same syntax.
42 struct spec
44 unsigned int directives;
45 unsigned int arg_count;
46 bool args_used[9];
50 static void *
51 format_parse (const char *format, bool translated, char **invalid_reason)
53 struct spec spec;
54 struct spec *result;
56 spec.directives = 0;
57 spec.arg_count = 0;
59 for (; *format != '\0';)
60 if (*format++ == '%')
62 /* A directive. */
63 spec.directives++;
65 if (*format == '%')
66 format++;
67 else if (*format >= '1' && *format <= '9')
69 unsigned int number = *format - '1';
71 while (spec.arg_count <= number)
72 spec.args_used[spec.arg_count++] = false;
73 spec.args_used[number] = true;
75 format++;
77 else
79 *invalid_reason =
80 (*format == '\0'
81 ? INVALID_UNTERMINATED_DIRECTIVE ()
82 : (c_isprint (*format)
83 ? xasprintf (_("In the directive number %u, the character '%c' is not a digit between 1 and 9."), spec.directives, *format)
84 : xasprintf (_("The character that terminates the directive number %u is not a digit between 1 and 9."), spec.directives)));
85 goto bad_format;
89 result = (struct spec *) xmalloc (sizeof (struct spec));
90 *result = spec;
91 return result;
93 bad_format:
94 return NULL;
97 static void
98 format_free (void *descr)
100 struct spec *spec = (struct spec *) descr;
102 free (spec);
105 static int
106 format_get_number_of_directives (void *descr)
108 struct spec *spec = (struct spec *) descr;
110 return spec->directives;
113 static bool
114 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
115 formatstring_error_logger_t error_logger,
116 const char *pretty_msgstr)
118 struct spec *spec1 = (struct spec *) msgid_descr;
119 struct spec *spec2 = (struct spec *) msgstr_descr;
120 bool err = false;
121 unsigned int i;
123 for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
125 bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
126 bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
128 if (equality ? (arg_used1 != arg_used2) : (!arg_used1 && arg_used2))
130 if (error_logger)
131 error_logger (arg_used1
132 ? _("a format specification for argument %u doesn't exist in '%s'")
133 : _("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
134 i + 1, pretty_msgstr);
135 err = true;
136 break;
140 return err;
144 struct formatstring_parser formatstring_ycp =
146 format_parse,
147 format_free,
148 format_get_number_of_directives,
149 format_check
153 struct formatstring_parser formatstring_smalltalk =
155 format_parse,
156 format_free,
157 format_get_number_of_directives,
158 format_check
162 #ifdef TEST
164 /* Test program: Print the argument list specification returned by
165 format_parse for strings read from standard input. */
167 #include <stdio.h>
168 #include "getline.h"
170 static void
171 format_print (void *descr)
173 struct spec *spec = (struct spec *) descr;
174 unsigned int i;
176 if (spec == NULL)
178 printf ("INVALID");
179 return;
182 printf ("(");
183 for (i = 0; i < spec->arg_count; i++)
185 if (i > 0)
186 printf (" ");
187 if (spec->args_used[i])
188 printf ("*");
189 else
190 printf ("_");
192 printf (")");
196 main ()
198 for (;;)
200 char *line = NULL;
201 size_t line_size = 0;
202 int line_len;
203 char *invalid_reason;
204 void *descr;
206 line_len = getline (&line, &line_size, stdin);
207 if (line_len < 0)
208 break;
209 if (line_len > 0 && line[line_len - 1] == '\n')
210 line[--line_len] = '\0';
212 invalid_reason = NULL;
213 descr = format_parse (line, false, &invalid_reason);
215 format_print (descr);
216 printf ("\n");
217 if (descr == NULL)
218 printf ("%s\n", invalid_reason);
220 free (invalid_reason);
221 free (line);
224 return 0;
228 * For Emacs M-x compile
229 * Local Variables:
230 * 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-ycp.c ../lib/libgettextlib.la"
231 * End:
234 #endif /* TEST */