1 /* YCP and Smalltalk format strings.
2 Copyright (C) 2001-2004, 2006-2007, 2009, 2015 Free Software
4 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "xvasprintf.h"
30 #include "format-invalid.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'
38 GNU Smalltalk format strings are described in the CharArray documentation,
39 methods 'bindWith:' and 'bindWithArguments:'. They have the same syntax.
44 unsigned int directives;
45 unsigned int arg_count;
51 format_parse (const char *format, bool translated, char *fdi,
52 char **invalid_reason)
54 const char *const format_start = format;
61 for (; *format != '\0';)
65 FDI_SET (format - 1, FMTDIR_START);
70 else if (*format >= '1' && *format <= '9')
72 unsigned int number = *format - '1';
74 while (spec.arg_count <= number)
75 spec.args_used[spec.arg_count++] = false;
76 spec.args_used[number] = true;
84 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
85 FDI_SET (format - 1, FMTDIR_ERROR);
91 ? xasprintf (_("In the directive number %u, the character '%c' is not a digit between 1 and 9."), spec.directives, *format)
92 : xasprintf (_("The character that terminates the directive number %u is not a digit between 1 and 9."), spec.directives));
93 FDI_SET (format, FMTDIR_ERROR);
98 FDI_SET (format - 1, FMTDIR_END);
101 result = XMALLOC (struct spec);
110 format_free (void *descr)
112 struct spec *spec = (struct spec *) descr;
118 format_get_number_of_directives (void *descr)
120 struct spec *spec = (struct spec *) descr;
122 return spec->directives;
126 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
127 formatstring_error_logger_t error_logger,
128 const char *pretty_msgid, const char *pretty_msgstr)
130 struct spec *spec1 = (struct spec *) msgid_descr;
131 struct spec *spec2 = (struct spec *) msgstr_descr;
135 for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
137 bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
138 bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
140 if (equality ? (arg_used1 != arg_used2) : (!arg_used1 && arg_used2))
145 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
146 i + 1, pretty_msgstr);
148 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
149 i + 1, pretty_msgstr, pretty_msgid);
160 struct formatstring_parser formatstring_ycp =
164 format_get_number_of_directives,
170 struct formatstring_parser formatstring_smalltalk =
174 format_get_number_of_directives,
182 /* Test program: Print the argument list specification returned by
183 format_parse for strings read from standard input. */
188 format_print (void *descr)
190 struct spec *spec = (struct spec *) descr;
200 for (i = 0; i < spec->arg_count; i++)
204 if (spec->args_used[i])
218 size_t line_size = 0;
220 char *invalid_reason;
223 line_len = getline (&line, &line_size, stdin);
226 if (line_len > 0 && line[line_len - 1] == '\n')
227 line[--line_len] = '\0';
229 invalid_reason = NULL;
230 descr = format_parse (line, false, NULL, &invalid_reason);
232 format_print (descr);
235 printf ("%s\n", invalid_reason);
237 free (invalid_reason);
245 * For Emacs M-x compile
247 * 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"