1 /* braces.c -- code for doing word expansion in curly braces. */
3 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* Stuff in curly braces gets expanded before all other shell expansions. */
25 #if defined (BRACE_EXPANSION)
27 #if defined (HAVE_UNISTD_H)
29 # include <sys/types.h>
41 #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n')
44 extern char *extract_command_subst ();
49 Segregate the text into 3 sections: preamble (stuff before an open brace),
50 postamble (stuff after the matching close brace) and amble (stuff after
51 preamble, and before postamble). Expand amble, and then tack on the
52 expansions to preamble. Expand postamble, and tack on the expansions to
56 /* The character which is used to separate arguments. */
57 int brace_arg_separator = ',';
59 static int brace_gobbler ();
60 static char **expand_amble (), **array_concat ();
62 /* Return an array of strings; the brace expansion of TEXT. */
68 char *preamble, *postamble, *amble;
69 char **tack, **result;
72 /* Find the text of the preamble. */
74 c = brace_gobbler (text, &i, '{');
76 preamble = (char *)xmalloc (i + 1);
77 strncpy (preamble, text, i);
80 result = (char **)xmalloc (2 * sizeof (char *));
82 result[1] = (char *)NULL;
84 /* Special case. If we never found an exciting character, then
85 the preamble is all of the text, so just return that. */
89 /* Find the amble. This is the stuff inside this set of braces. */
91 c = brace_gobbler (text, &i, '}');
93 /* What if there isn't a matching close brace? */
97 /* Well, if we found an unquoted BRACE_ARG_SEPARATOR between START
98 and I, then this should be an error. Otherwise, it isn't. */
99 for (j = start; j < i; j++)
107 if (text[j] == brace_arg_separator)
110 report_error ("missing `}'");
111 throw_to_top_level ();
115 free (preamble); /* Same as result[0]; see initialization. */
116 result[0] = savestring (text);
120 amble = (char *)xmalloc (1 + (i - start));
121 strncpy (amble, &text[start], (i - start));
122 amble[i - start] = '\0';
125 /* If the amble does not contain an unquoted BRACE_ARG_SEPARATOR, then
126 just return without doing any expansion. */
127 for (j = 0; amble[j]; j++)
129 if (amble[j] == '\\')
134 if (amble[j] == brace_arg_separator)
142 result[0] = savestring (text);
147 postamble = &text[i + 1];
149 tack = expand_amble (amble);
150 result = array_concat (result, tack);
154 tack = brace_expand (postamble);
155 result = array_concat (result, tack);
161 /* Expand the text found inside of braces. We simply try to split the
162 text at BRACE_ARG_SEPARATORs into separate strings. We then brace
163 expand each slot which needs it, until there are no more slots which
169 char **result, **partial;
173 result = (char **)NULL;
175 for (start = 0, i = 0, c = 1; c; start = ++i)
177 c = brace_gobbler (text, &i, brace_arg_separator);
178 tem = (char *)xmalloc (1 + (i - start));
179 strncpy (tem, &text[start], (i - start));
180 tem[i- start] = '\0';
182 partial = brace_expand (tem);
188 register int lr = array_len (result);
189 register int lp = array_len (partial);
192 result = (char **)xrealloc (result, (1 + lp + lr) * sizeof (char *));
194 for (j = 0; j < lp; j++)
195 result[lr + j] = partial[j];
197 result[lr + j] = (char *)NULL;
205 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the
206 index of the character matching SATISFY. This understands about
207 quoting. Return the character that caused us to stop searching;
208 this is either the same as SATISFY, or 0. */
210 brace_gobbler (text, indx, satisfy)
215 register int i, c, quoted, level, pass_next;
221 level = quoted = pass_next = 0;
223 for (i = *indx; c = text[i]; i++)
231 /* A backslash escapes the next character. This allows backslash to
232 escape the quote character in a double-quoted string. */
233 if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`'))
246 if (c == '"' || c == '\'' || c == '`')
253 /* Pass new-style command substitutions through unchanged. */
254 if (c == '$' && text[i+1] == '(') /* ) */
257 t = extract_command_subst (text, &si);
264 if (c == satisfy && level == 0 && quoted == 0)
266 /* We ignore an open brace surrounded by whitespace, and also
267 an open brace followed immediately by a close brace preceded
270 ((!i || brace_whitespace (text[i - 1])) &&
271 (brace_whitespace (text[i + 1]) || text[i + 1] == '}')))
274 /* If this is being compiled as part of bash, ignore the `{'
275 in a `${}' construct */
276 if ((c != '{') || i == 0 || (text[i - 1] != '$'))
283 else if (c == '}' && level)
291 /* Return a new array of strings which is the result of appending each
292 string in ARR2 to each string in ARR1. The resultant array is
293 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents)
294 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2
297 array_concat (arr1, arr2)
300 register int i, j, len, len1, len2;
301 register char **result;
304 return (copy_array (arr2));
307 return (copy_array (arr1));
309 len1 = array_len (arr1);
310 len2 = array_len (arr2);
312 result = (char **)xmalloc ((1 + (len1 * len2)) * sizeof (char *));
315 for (i = 0; i < len1; i++)
317 int strlen_1 = strlen (arr1[i]);
319 for (j = 0; j < len2; j++)
322 (char *)xmalloc (1 + strlen_1 + strlen (arr2[j]));
323 strcpy (result[len], arr1[i]);
324 strcpy (result[len] + strlen_1, arr2[j]);
331 result[len] = (char *)NULL;
338 fatal_error (format, arg1, arg2)
339 char *format, *arg1, *arg2;
341 report_error (format, arg1, arg2);
345 report_error (format, arg1, arg2)
346 char *format, *arg1, *arg2;
348 fprintf (stderr, format, arg1, arg2);
349 fprintf (stderr, "\n");
361 fprintf (stderr, "brace_expand> ");
363 if ((!fgets (example, 256, stdin)) ||
364 (strncmp (example, "quit", 4) == 0))
367 if (strlen (example))
368 example[strlen (example) - 1] = '\0';
370 result = brace_expand (example);
372 for (i = 0; result[i]; i++)
373 printf ("%s\n", result[i]);
381 * compile-command: "gcc -g -Bstatic -DTEST -o brace_expand braces.c general.o"
386 #endif /* BRACE_EXPANSION */