Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / braces.c
1 /* braces.c -- code for doing word expansion in curly braces. */
2
3 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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 2, or (at your option)
10    any later version.
11
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.
16
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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 /* Stuff in curly braces gets expanded before all other shell expansions. */
22
23 #include "config.h"
24
25 #if defined (BRACE_EXPANSION)
26
27 #if defined (HAVE_UNISTD_H)
28 #  ifdef _MINIX
29 #    include <sys/types.h>
30 #  endif
31 #  include <unistd.h>
32 #endif
33
34 #include "bashansi.h"
35
36 #if defined (SHELL)
37 #  include "shell.h"
38 #endif /* SHELL */
39
40 #include "general.h"
41 #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n')
42
43 /* Basic idea:
44
45    Segregate the text into 3 sections: preamble (stuff before an open brace),
46    postamble (stuff after the matching close brace) and amble (stuff after
47    preamble, and before postamble).  Expand amble, and then tack on the
48    expansions to preamble.  Expand postamble, and tack on the expansions to
49    the result so far.
50  */
51
52 /* The character which is used to separate arguments. */
53 int brace_arg_separator = ',';
54
55 #if defined (__P)
56 static int brace_gobbler __P((char *, int *, int));
57 static char **expand_amble __P((char *));
58 static char **array_concat __P((char **, char **));
59 #else
60 static int brace_gobbler ();
61 static char **expand_amble ();
62 static char **array_concat ();
63 #endif
64
65 /* Return an array of strings; the brace expansion of TEXT. */
66 char **
67 brace_expand (text)
68      char *text;
69 {
70   register int start;
71   char *preamble, *postamble, *amble;
72   char **tack, **result;
73   int i, j, c;
74
75   /* Find the text of the preamble. */
76   i = 0;
77   c = brace_gobbler (text, &i, '{');
78
79   preamble = (char *)xmalloc (i + 1);
80   strncpy (preamble, text, i);
81   preamble[i] = '\0';
82
83   result = (char **)xmalloc (2 * sizeof (char *));
84   result[0] = preamble;
85   result[1] = (char *)NULL;
86
87   /* Special case.  If we never found an exciting character, then
88      the preamble is all of the text, so just return that. */
89   if (c != '{')
90     return (result);
91
92   /* Find the amble.  This is the stuff inside this set of braces. */
93   start = ++i;
94   c = brace_gobbler (text, &i, '}');
95
96   /* What if there isn't a matching close brace? */
97   if (c == 0)
98     {
99 #if defined (NOTDEF)
100       /* Well, if we found an unquoted BRACE_ARG_SEPARATOR between START
101          and I, then this should be an error.  Otherwise, it isn't. */
102       for (j = start; j < i; j++)
103         {
104           if (text[j] == '\\')
105             {
106               j++;
107               continue;
108             }
109
110           if (text[j] == brace_arg_separator)
111             {
112               free_array (result);
113               report_error ("missing `}'");
114               throw_to_top_level ();
115             }
116         }
117 #endif
118       free (preamble);          /* Same as result[0]; see initialization. */
119       result[0] = savestring (text);
120       return (result);
121     }
122
123 #if defined (SHELL)
124   amble = substring (text, start, i);
125 #else
126   amble = (char *)xmalloc (1 + (i - start));
127   strncpy (amble, &text[start], (i - start));
128   amble[i - start] = '\0';
129 #endif
130
131 #if defined (SHELL)
132   /* If the amble does not contain an unquoted BRACE_ARG_SEPARATOR, then
133      just return without doing any expansion.  */
134   for (j = 0; amble[j]; j++)
135     {
136       if (amble[j] == '\\')
137         {
138           j++;
139           continue;
140         }
141       if (amble[j] == brace_arg_separator)
142         break;
143     }
144
145   if (!amble[j])
146     {
147       free (amble);
148       free (preamble);
149       result[0] = savestring (text);
150       return (result);
151     }
152 #endif /* SHELL */
153
154   postamble = &text[i + 1];
155
156   tack = expand_amble (amble);
157   result = array_concat (result, tack);
158   free (amble);
159   free_array (tack);
160
161   tack = brace_expand (postamble);
162   result = array_concat (result, tack);
163   free_array (tack);
164
165   return (result);
166 }
167
168 /* Expand the text found inside of braces.  We simply try to split the
169    text at BRACE_ARG_SEPARATORs into separate strings.  We then brace
170    expand each slot which needs it, until there are no more slots which
171    need it. */
172 static char **
173 expand_amble (text)
174      char *text;
175 {
176   char **result, **partial;
177   char *tem;
178   int start, i, c;
179
180   result = (char **)NULL;
181
182   for (start = 0, i = 0, c = 1; c; start = ++i)
183     {
184       c = brace_gobbler (text, &i, brace_arg_separator);
185 #if defined (SHELL)
186       tem = substring (text, start, i);
187 #else
188       tem = (char *)xmalloc (1 + (i - start));
189       strncpy (tem, &text[start], (i - start));
190       tem[i- start] = '\0';
191 #endif
192
193       partial = brace_expand (tem);
194
195       if (!result)
196         result = partial;
197       else
198         {
199           register int lr = array_len (result);
200           register int lp = array_len (partial);
201           register int j;
202
203           result = (char **)xrealloc (result, (1 + lp + lr) * sizeof (char *));
204
205           for (j = 0; j < lp; j++)
206             result[lr + j] = partial[j];
207
208           result[lr + j] = (char *)NULL;
209           free (partial);
210         }
211       free (tem);
212     }
213   return (result);
214 }
215
216 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the
217    index of the character matching SATISFY.  This understands about
218    quoting.  Return the character that caused us to stop searching;
219    this is either the same as SATISFY, or 0. */
220 static int
221 brace_gobbler (text, indx, satisfy)
222      char *text;
223      int *indx;
224      int satisfy;
225 {
226   register int i, c, quoted, level, pass_next;
227 #if defined (SHELL)
228   int si;
229   char *t;
230 #endif
231
232   level = quoted = pass_next = 0;
233
234   for (i = *indx; c = text[i]; i++)
235     {
236       if (pass_next)
237         {
238           pass_next = 0;
239           continue;
240         }
241
242       /* A backslash escapes the next character.  This allows backslash to
243          escape the quote character in a double-quoted string. */
244       if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`'))
245         {
246           pass_next = 1;
247           continue;
248         }
249
250       if (quoted)
251         {
252           if (c == quoted)
253             quoted = 0;
254           continue;
255         }
256
257       if (c == '"' || c == '\'' || c == '`')
258         {
259           quoted = c;
260           continue;
261         }
262
263 #if defined (SHELL)
264       /* Pass new-style command substitutions through unchanged. */
265       if (c == '$' && text[i+1] == '(')                 /* ) */
266         {
267           si = i + 2;
268           t = extract_command_subst (text, &si);
269           i = si;
270           free (t);
271           continue;
272         }
273 #endif
274
275       if (c == satisfy && level == 0 && quoted == 0)
276         {
277           /* We ignore an open brace surrounded by whitespace, and also
278              an open brace followed immediately by a close brace preceded
279              by whitespace.  */
280           if (c == '{' &&
281               ((!i || brace_whitespace (text[i - 1])) &&
282                (brace_whitespace (text[i + 1]) || text[i + 1] == '}')))
283             continue;
284 #if defined (SHELL)
285           /* If this is being compiled as part of bash, ignore the `{'
286              in a `${}' construct */
287           if ((c != '{') || i == 0 || (text[i - 1] != '$'))
288 #endif /* SHELL */
289             break;
290         }
291
292       if (c == '{')
293         level++;
294       else if (c == '}' && level)
295         level--;
296     }
297
298   *indx = i;
299   return (c);
300 }
301
302 /* Return a new array of strings which is the result of appending each
303    string in ARR2 to each string in ARR1.  The resultant array is
304    len (arr1) * len (arr2) long.  For convenience, ARR1 (and its contents)
305    are free ()'ed.  ARR1 can be NULL, in that case, a new version of ARR2
306    is returned. */
307 static char **
308 array_concat (arr1, arr2)
309      char **arr1, **arr2;
310 {
311   register int i, j, len, len1, len2;
312   register char **result;
313
314   if (arr1 == 0)
315     return (copy_array (arr2));
316
317   if (arr2 == 0)
318     return (copy_array (arr1));
319
320   len1 = array_len (arr1);
321   len2 = array_len (arr2);
322
323   result = (char **)xmalloc ((1 + (len1 * len2)) * sizeof (char *));
324
325   len = 0;
326   for (i = 0; i < len1; i++)
327     {
328       int strlen_1 = strlen (arr1[i]);
329
330       for (j = 0; j < len2; j++)
331         {
332           result[len] =
333             (char *)xmalloc (1 + strlen_1 + strlen (arr2[j]));
334           strcpy (result[len], arr1[i]);
335           strcpy (result[len] + strlen_1, arr2[j]);
336           len++;
337         }
338       free (arr1[i]);
339     }
340   free (arr1);
341
342   result[len] = (char *)NULL;
343   return (result);
344 }
345
346 #if defined (TEST)
347 #include <stdio.h>
348
349 fatal_error (format, arg1, arg2)
350      char *format, *arg1, *arg2;
351 {
352   report_error (format, arg1, arg2);
353   exit (1);
354 }
355
356 report_error (format, arg1, arg2)
357      char *format, *arg1, *arg2;
358 {
359   fprintf (stderr, format, arg1, arg2);
360   fprintf (stderr, "\n");
361 }
362
363 main ()
364 {
365   char example[256];
366
367   for (;;)
368     {
369       char **result;
370       int i;
371
372       fprintf (stderr, "brace_expand> ");
373
374       if ((!fgets (example, 256, stdin)) ||
375           (strncmp (example, "quit", 4) == 0))
376         break;
377
378       if (strlen (example))
379         example[strlen (example) - 1] = '\0';
380
381       result = brace_expand (example);
382
383       for (i = 0; result[i]; i++)
384         printf ("%s\n", result[i]);
385
386       free_array (result);
387     }
388 }
389 \f
390 /*
391  * Local variables:
392  * compile-command: "gcc -g -Bstatic -DTEST -o brace_expand braces.c general.o"
393  * end:
394  */
395
396 #endif /* TEST */
397 #endif /* BRACE_EXPANSION */