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