1 /* Shell format strings.
2 Copyright (C) 2003-2004, 2006-2007, 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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/>. */
29 #include "format-invalid.h"
32 #define _(str) gettext (str)
34 /* Shell format strings are simply strings subjects to variable substitution.
35 A variable substitution starts with '$' and is finished by either
36 - a nonempty sequence of alphanumeric ASCII characters, the first being
38 - an opening brace '{', a nonempty sequence of alphanumeric ASCII
39 characters, the first being not a digit, and a closing brace '}'.
40 We don't support variable references like $1, $$ or $? since they make
41 no sense when 'envsubst' is invoked.
42 We don't support non-ASCII variable names, to avoid dependencies w.r.t. the
43 current encoding: While "${\xe0}" looks like a variable access in ISO-8859-1
44 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,
45 SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these
47 We don't support the POSIX syntax for default or alternate values:
48 ${variable-default} ${variable:-default}
49 ${variable=default} ${variable:=default}
50 ${variable+replacement} ${variable:+replacement}
51 ${variable?ignored} ${variable:?ignored}
52 because the translator might be tempted to change the default value; if
53 we allow it we have a security problem; if we don't allow it the translator
64 unsigned int directives;
65 unsigned int named_arg_count;
66 unsigned int allocated;
67 struct named_arg *named;
72 named_arg_compare (const void *p1, const void *p2)
74 return strcmp (((const struct named_arg *) p1)->name,
75 ((const struct named_arg *) p2)->name);
78 #define INVALID_NON_ASCII_VARIABLE() \
79 xstrdup (_("The string refers to a shell variable with a non-ASCII name."))
80 #define INVALID_SHELL_SYNTAX() \
81 xstrdup (_("The string refers to a shell variable with complex shell brace syntax. This syntax is unsupported here due to security reasons."))
82 #define INVALID_CONTEXT_DEPENDENT_VARIABLE() \
83 xstrdup (_("The string refers to a shell variable whose value may be different inside shell functions."))
84 #define INVALID_EMPTY_VARIABLE() \
85 xstrdup (_("The string refers to a shell variable with an empty name."))
88 format_parse (const char *format, bool translated, char *fdi,
89 char **invalid_reason)
91 const char *const format_start = format;
96 spec.named_arg_count = 0;
100 for (; *format != '\0';)
101 if (*format++ == '$')
103 /* A variable substitution. */
106 FDI_SET (format - 1, FMTDIR_START);
111 const char *name_start;
112 const char *name_end;
115 name_start = ++format;
116 for (; *format != '\0'; format++)
120 if (!c_isascii (*format))
122 *invalid_reason = INVALID_NON_ASCII_VARIABLE ();
123 FDI_SET (format, FMTDIR_ERROR);
126 if (format > name_start
127 && (*format == '-' || *format == '=' || *format == '+'
128 || *format == '?' || *format == ':'))
130 *invalid_reason = INVALID_SHELL_SYNTAX ();
131 FDI_SET (format, FMTDIR_ERROR);
134 if (!(c_isalnum (*format) || *format == '_')
135 || (format == name_start && c_isdigit (*format)))
137 *invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
138 FDI_SET (format, FMTDIR_ERROR);
144 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
145 FDI_SET (format - 1, FMTDIR_ERROR);
150 n = name_end - name_start;
153 *invalid_reason = INVALID_EMPTY_VARIABLE ();
154 FDI_SET (format - 1, FMTDIR_ERROR);
157 name = XNMALLOC (n + 1, char);
158 memcpy (name, name_start, n);
161 else if (c_isalpha (*format) || *format == '_')
163 const char *name_start;
164 const char *name_end;
170 while (*format != '\0' && (c_isalnum (*format) || *format == '_'));
173 n = name_end - name_start;
174 name = XNMALLOC (n + 1, char);
175 memcpy (name, name_start, n);
178 else if (*format != '\0')
180 if (!c_isascii (*format))
182 *invalid_reason = INVALID_NON_ASCII_VARIABLE ();
183 FDI_SET (format, FMTDIR_ERROR);
188 *invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
189 FDI_SET (format, FMTDIR_ERROR);
195 *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
196 FDI_SET (format - 1, FMTDIR_ERROR);
200 /* Named argument. */
201 if (spec.allocated == spec.named_arg_count)
203 spec.allocated = 2 * spec.allocated + 1;
204 spec.named = (struct named_arg *) xrealloc (spec.named, spec.allocated * sizeof (struct named_arg));
206 spec.named[spec.named_arg_count].name = name;
207 spec.named_arg_count++;
209 FDI_SET (format - 1, FMTDIR_END);
212 /* Sort the named argument array, and eliminate duplicates. */
213 if (spec.named_arg_count > 1)
217 qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg),
220 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
221 for (i = j = 0; i < spec.named_arg_count; i++)
222 if (j > 0 && strcmp (spec.named[i].name, spec.named[j-1].name) == 0)
223 free (spec.named[i].name);
227 spec.named[j].name = spec.named[i].name;
230 spec.named_arg_count = j;
233 result = XMALLOC (struct spec);
238 if (spec.named != NULL)
241 for (i = 0; i < spec.named_arg_count; i++)
242 free (spec.named[i].name);
249 format_free (void *descr)
251 struct spec *spec = (struct spec *) descr;
253 if (spec->named != NULL)
256 for (i = 0; i < spec->named_arg_count; i++)
257 free (spec->named[i].name);
264 format_get_number_of_directives (void *descr)
266 struct spec *spec = (struct spec *) descr;
268 return spec->directives;
272 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
273 formatstring_error_logger_t error_logger,
274 const char *pretty_msgid, const char *pretty_msgstr)
276 struct spec *spec1 = (struct spec *) msgid_descr;
277 struct spec *spec2 = (struct spec *) msgstr_descr;
280 if (spec1->named_arg_count + spec2->named_arg_count > 0)
283 unsigned int n1 = spec1->named_arg_count;
284 unsigned int n2 = spec2->named_arg_count;
286 /* Check the argument names are the same.
287 Both arrays are sorted. We search for the first difference. */
288 for (i = 0, j = 0; i < n1 || j < n2; )
290 int cmp = (i >= n1 ? 1 :
292 strcmp (spec1->named[i].name, spec2->named[j].name));
297 error_logger (_("a format specification for argument '%s', as in '%s', doesn't exist in '%s'"),
298 spec2->named[j].name, pretty_msgstr,
308 error_logger (_("a format specification for argument '%s' doesn't exist in '%s'"),
309 spec1->named[i].name, pretty_msgstr);
325 struct formatstring_parser formatstring_sh =
329 format_get_number_of_directives,
337 /* Test program: Print the argument list specification returned by
338 format_parse for strings read from standard input. */
343 format_print (void *descr)
345 struct spec *spec = (struct spec *) descr;
355 for (i = 0; i < spec->named_arg_count; i++)
359 printf ("'%s'", spec->named[i].name);
370 size_t line_size = 0;
372 char *invalid_reason;
375 line_len = getline (&line, &line_size, stdin);
378 if (line_len > 0 && line[line_len - 1] == '\n')
379 line[--line_len] = '\0';
381 invalid_reason = NULL;
382 descr = format_parse (line, false, NULL, &invalid_reason);
384 format_print (descr);
387 printf ("%s\n", invalid_reason);
389 free (invalid_reason);
397 * For Emacs M-x compile
399 * 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-sh.c ../gnulib-lib/libgettextlib.la"