home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / braces.c < prev    next >
C/C++ Source or Header  |  1991-07-07  |  8KB  |  351 lines

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