1 /* YCP and Smalltalk format strings.
2 Copyright (C) 2001-2004, 2006-2007, 2009 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 3 of the License, or
8 (at your option) 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, see <http://www.gnu.org/licenses/>. */
28 #include "xvasprintf.h"
29 #include "format-invalid.h"
32 #define _(str) gettext (str)
34 /* YCP sformat strings are described in libycp documentation YCP-builtins.html.
35 A directive starts with '%' and is followed by '%' or a nonzero digit ('1'
37 GNU Smalltalk format strings are described in the CharArray documentation,
38 methods 'bindWith:' and 'bindWithArguments:'. They have the same syntax.
43 unsigned int directives;
44 unsigned int arg_count;
50 format_parse (const char *format, bool translated, char *fdi,
51 char **invalid_reason)
53 const char *const format_start = format;
60 for (; *format != '\0';)
64 FDI_SET (format - 1, FMTDIR_START);
69 else if (*format >= '1' && *format <= '9')
71 unsigned int number = *format - '1';
73 while (spec.arg_count <= number)
74 spec.args_used[spec.arg_count++] = false;
75 spec.args_used[number] = true;
83 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
84 FDI_SET (format - 1, FMTDIR_ERROR);
90 ? xasprintf (_("In the directive number %u, the character '%c' is not a digit between 1 and 9."), spec.directives, *format)
91 : xasprintf (_("The character that terminates the directive number %u is not a digit between 1 and 9."), spec.directives));
92 FDI_SET (format, FMTDIR_ERROR);
97 FDI_SET (format - 1, FMTDIR_END);
100 result = XMALLOC (struct spec);
109 format_free (void *descr)
111 struct spec *spec = (struct spec *) descr;
117 format_get_number_of_directives (void *descr)
119 struct spec *spec = (struct spec *) descr;
121 return spec->directives;
125 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
126 formatstring_error_logger_t error_logger,
127 const char *pretty_msgid, const char *pretty_msgstr)
129 struct spec *spec1 = (struct spec *) msgid_descr;
130 struct spec *spec2 = (struct spec *) msgstr_descr;
134 for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
136 bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
137 bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
139 if (equality ? (arg_used1 != arg_used2) : (!arg_used1 && arg_used2))
144 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
145 i + 1, pretty_msgstr);
147 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
148 i + 1, pretty_msgstr, pretty_msgid);
159 struct formatstring_parser formatstring_ycp =
163 format_get_number_of_directives,
169 struct formatstring_parser formatstring_smalltalk =
173 format_get_number_of_directives,
181 /* Test program: Print the argument list specification returned by
182 format_parse for strings read from standard input. */
187 format_print (void *descr)
189 struct spec *spec = (struct spec *) descr;
199 for (i = 0; i < spec->arg_count; i++)
203 if (spec->args_used[i])
217 size_t line_size = 0;
219 char *invalid_reason;
222 line_len = getline (&line, &line_size, stdin);
225 if (line_len > 0 && line[line_len - 1] == '\n')
226 line[--line_len] = '\0';
228 invalid_reason = NULL;
229 descr = format_parse (line, false, NULL, &invalid_reason);
231 format_print (descr);
234 printf ("%s\n", invalid_reason);
236 free (invalid_reason);
244 * For Emacs M-x compile
246 * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../intl -DHAVE_CONFIG_H -DTEST format-ycp.c ../gnulib-lib/libgettextlib.la"