1
/* Shell format strings.
2
Copyright (C) 2003-2004 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 2, or (at your option)
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. */
30
#include "format-invalid.h"
32
#include "error-progname.h"
35
#define _(str) gettext (str)
37
/* Shell format strings are simply strings subjects to variable substitution.
38
A variable substitution starts with '$' and is finished by either
39
- a nonempty sequence of alphanumeric ASCII characters, the first being
41
- an opening brace '{', a nonempty sequence of alphanumeric ASCII
42
characters, the first being not a digit, and a closing brace '}'.
43
We don't support variable references like $1, $$ or $? since they make
44
no sense when 'envsubst' is invoked.
45
We don't support non-ASCII variable names, to avoid dependencies w.r.t. the
46
current encoding: While "${\xe0}" looks like a variable access in ISO-8859-1
47
encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,
48
SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these
50
We don't support the POSIX syntax for default or alternate values:
51
${variable-default} ${variable:-default}
52
${variable=default} ${variable:=default}
53
${variable+replacement} ${variable:+replacement}
54
${variable?ignored} ${variable:?ignored}
55
because the translator might be tempted to change the default value; if
56
we allow it we have a security problem; if we don't allow it the translator
67
unsigned int directives;
68
unsigned int named_arg_count;
69
unsigned int allocated;
70
struct named_arg *named;
75
named_arg_compare (const void *p1, const void *p2)
77
return strcmp (((const struct named_arg *) p1)->name,
78
((const struct named_arg *) p2)->name);
81
#define INVALID_NON_ASCII_VARIABLE() \
82
xstrdup (_("The string refers to a shell variable with a non-ASCII name."))
83
#define INVALID_SHELL_SYNTAX() \
84
xstrdup (_("The string refers to a shell variable with complex shell brace syntax. This syntax is unsupported here due to security reasons."))
85
#define INVALID_CONTEXT_DEPENDENT_VARIABLE() \
86
xstrdup (_("The string refers to a shell variable whose value may be different inside shell functions."))
87
#define INVALID_EMPTY_VARIABLE() \
88
xstrdup (_("The string refers to a shell variable with an empty name."))
91
format_parse (const char *format, bool translated, char **invalid_reason)
97
spec.named_arg_count = 0;
101
for (; *format != '\0';)
102
if (*format++ == '$')
104
/* A variable substitution. */
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 ();
125
if (format > name_start
126
&& (*format == '-' || *format == '=' || *format == '+'
127
|| *format == '?' || *format == ':'))
129
*invalid_reason = INVALID_SHELL_SYNTAX ();
132
if (!(c_isalnum (*format) || *format == '_')
133
|| (format == name_start && c_isdigit (*format)))
135
*invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
141
*invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
146
n = name_end - name_start;
149
*invalid_reason = INVALID_EMPTY_VARIABLE ();
152
name = (char *) xmalloc (n + 1);
153
memcpy (name, name_start, n);
156
else if (c_isalpha (*format) || *format == '_')
158
const char *name_start;
159
const char *name_end;
165
while (*format != '\0' && (c_isalnum (*format) || *format == '_'));
168
n = name_end - name_start;
169
name = (char *) xmalloc (n + 1);
170
memcpy (name, name_start, n);
173
else if (*format != '\0')
175
if (!c_isascii (*format))
177
*invalid_reason = INVALID_NON_ASCII_VARIABLE ();
182
*invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
188
*invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
192
/* Named argument. */
193
if (spec.allocated == spec.named_arg_count)
195
spec.allocated = 2 * spec.allocated + 1;
196
spec.named = (struct named_arg *) xrealloc (spec.named, spec.allocated * sizeof (struct named_arg));
198
spec.named[spec.named_arg_count].name = name;
199
spec.named_arg_count++;
202
/* Sort the named argument array, and eliminate duplicates. */
203
if (spec.named_arg_count > 1)
207
qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg),
210
/* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
211
for (i = j = 0; i < spec.named_arg_count; i++)
212
if (j > 0 && strcmp (spec.named[i].name, spec.named[j-1].name) == 0)
213
free (spec.named[i].name);
217
spec.named[j].name = spec.named[i].name;
220
spec.named_arg_count = j;
223
result = (struct spec *) xmalloc (sizeof (struct spec));
228
if (spec.named != NULL)
231
for (i = 0; i < spec.named_arg_count; i++)
232
free (spec.named[i].name);
239
format_free (void *descr)
241
struct spec *spec = (struct spec *) descr;
243
if (spec->named != NULL)
246
for (i = 0; i < spec->named_arg_count; i++)
247
free (spec->named[i].name);
254
format_get_number_of_directives (void *descr)
256
struct spec *spec = (struct spec *) descr;
258
return spec->directives;
262
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
263
bool equality, bool noisy, const char *pretty_msgstr)
265
struct spec *spec1 = (struct spec *) msgid_descr;
266
struct spec *spec2 = (struct spec *) msgstr_descr;
269
if (spec1->named_arg_count + spec2->named_arg_count > 0)
272
unsigned int n1 = spec1->named_arg_count;
273
unsigned int n2 = spec2->named_arg_count;
275
/* Check the argument names are the same.
276
Both arrays are sorted. We search for the first difference. */
277
for (i = 0, j = 0; i < n1 || j < n2; )
279
int cmp = (i >= n1 ? 1 :
281
strcmp (spec1->named[i].name, spec2->named[j].name));
287
error_with_progname = false;
288
error_at_line (0, 0, pos->file_name, pos->line_number,
289
_("a format specification for argument '%s', as in '%s', doesn't exist in 'msgid'"),
290
spec2->named[j].name, pretty_msgstr);
291
error_with_progname = true;
302
error_with_progname = false;
303
error_at_line (0, 0, pos->file_name, pos->line_number,
304
_("a format specification for argument '%s' doesn't exist in '%s'"),
305
spec1->named[i].name, pretty_msgstr);
306
error_with_progname = true;
323
struct formatstring_parser formatstring_sh =
327
format_get_number_of_directives,
334
/* Test program: Print the argument list specification returned by
335
format_parse for strings read from standard input. */
341
format_print (void *descr)
343
struct spec *spec = (struct spec *) descr;
353
for (i = 0; i < spec->named_arg_count; i++)
357
printf ("'%s'", spec->named[i].name);
368
size_t line_size = 0;
370
char *invalid_reason;
373
line_len = getline (&line, &line_size, stdin);
376
if (line_len > 0 && line[line_len - 1] == '\n')
377
line[--line_len] = '\0';
379
invalid_reason = NULL;
380
descr = format_parse (line, false, &invalid_reason);
382
format_print (descr);
385
printf ("%s\n", invalid_reason);
387
free (invalid_reason);
395
* For Emacs M-x compile
397
* 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-sh.c ../lib/libgettextlib.la"