]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - bracecomp.c
Bash-4.2 patch 40
[thirdparty/bash.git] / bracecomp.c
index c07e15df37b0a1aa2b8f634f93b49c15771c58f2..cb218a2bf42d25c8799ea0e82754a2d0c317c3dc 100644 (file)
@@ -4,35 +4,45 @@
 
 /* Original version by tromey@cns.caltech.edu,  Fri Feb  7 1992. */
 
-/* Copyright (C) 1993 Free Software Foundation, Inc.
+/* Copyright (C) 1993-2009 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   Bash is free software; you can redistribute it and/or modify it under
-   the terms of the GNU General Public License as published by the Free
-   Software Foundation; either version 2, or (at your option) any later
-   version.
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-   WARRANTY; without even the implied warranty of MERCHANTABILITY or
-   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-   for more details.
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   You should have received a copy of the GNU General Public License along
-   with Bash; see the file COPYING.  If not, write to the Free Software
-   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "config.h"
+
+#if defined (BRACE_EXPANSION) && defined (READLINE)
 
 #include <stdio.h>
 
-#if defined (HAVE_STRING_H)
-#  include <string.h>
-#else /* !HAVE_STRING_H */
-#  include <strings.h>
-#endif /* !HAVE_STRING_H */
+#if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
+#  include <unistd.h>
+#endif
+
+#include "bashansi.h"
+#include "shmbutil.h"
 
 #include "shell.h"
 #include <readline/readline.h>
 
+static int _strcompare __P((char **, char **));
+
 /* Find greatest common prefix of two strings. */
 static int
 string_gcd (s1, s2)
@@ -58,20 +68,19 @@ really_munge_braces (array, real_start, real_end, gcd_zero)
      int real_start, real_end, gcd_zero;
 {
   int start, end, gcd;
-  char *result, *subterm;
-  int result_size, flag;
+  char *result, *subterm, *x;
+  int result_size, flag, tlen;
 
   flag = 0;
 
   if (real_start == real_end)
     {
-      if (array[real_start])
-        return (savestring (array[real_start] + gcd_zero));
-      else
-        return (savestring (array[0]));
+      x = array[real_start] ? sh_backslash_quote (array[real_start] + gcd_zero)
+                           : sh_backslash_quote (array[0]);
+      return x;
     }
 
-  result = (char *) xmalloc (result_size = 1);
+  result = (char *)xmalloc (result_size = 16);
   *result = '\0';
 
   for (start = real_start; start < real_end; start = end + 1)
@@ -95,27 +104,41 @@ really_munge_braces (array, real_start, real_end, gcd_zero)
          /* In this case, add in a leading '{', because we are at
             top level, and there isn't a consistent prefix. */
          result_size += 1;
-         result = (char *) xrealloc (result, result_size);
-         strcpy (result, "{");
+         result = (char *)xrealloc (result, result_size);
+         result[0] = '{'; result[1] = '\0';
          flag++;
        }
 
+      /* Make sure we backslash quote every substring we insert into the
+        resultant brace expression.  This is so the default filename
+        quoting function won't inappropriately quote the braces. */
       if (start == end)
-       subterm = savestring (array[start] + gcd_zero);
+       {
+         x = savestring (array[start] + gcd_zero);
+         subterm = sh_backslash_quote (x);
+         free (x);
+       }
       else
        {
          /* If there is more than one element in the subarray,
-            insert the prefix and an opening brace. */
-         result_size += gcd - gcd_zero + 1;
-         result = (char *) xrealloc (result, result_size);
-         strncat (result, array[start] + gcd_zero, gcd - gcd_zero);
+            insert the (quoted) prefix and an opening brace. */
+         tlen = gcd - gcd_zero;
+         x = (char *)xmalloc (tlen + 1);
+         strncpy (x, array[start] + gcd_zero, tlen);
+         x[tlen] = '\0';
+         subterm = sh_backslash_quote (x);
+         free (x);
+         result_size += strlen (subterm) + 1;
+         result = (char *)xrealloc (result, result_size);
+         strcat (result, subterm);
+         free (subterm);
          strcat (result, "{");
          subterm = really_munge_braces (array, start, end + 1, gcd);
          subterm[strlen (subterm) - 1] = '}';
        }
 
       result_size += strlen (subterm) + 1;
-      result = (char *) xrealloc (result, result_size);
+      result = (char *)xrealloc (result, result_size);
       strcat (result, subterm);
       strcat (result, ",");
       free (subterm);
@@ -126,14 +149,31 @@ really_munge_braces (array, real_start, real_end, gcd_zero)
   return (result);
 }
 
-static void
+static int
+_strcompare (s1, s2)
+     char **s1, **s2;
+{
+  int result;
+
+  result = **s1 - **s2;
+  if (result == 0)
+    result = strcmp (*s1, *s2);
+
+  return result;
+}
+
+static int
 hack_braces_completion (names)
      char **names;
 {
   register int i;
   char *temp;
 
-  temp = really_munge_braces (names, 1, array_len (names), 0);
+  i = strvec_len (names);
+  if (MB_CUR_MAX > 1 && i > 2)
+    qsort (names+1, i-1, sizeof (char *), (QSFUNC *)_strcompare);
+      
+  temp = really_munge_braces (names, 1, i, 0);
 
   for (i = 0; names[i]; ++i)
     {
@@ -141,26 +181,41 @@ hack_braces_completion (names)
       names[i] = NULL;
     }
   names[0] = temp;
+  return 0;
 }
 
-void
-bash_brace_completion ()
+/* We handle quoting ourselves within hack_braces_completion, so we turn off
+   rl_filename_quoting_desired and rl_filename_quoting_function. */
+int
+bash_brace_completion (count, ignore)
+     int count, ignore;
 {
-  Function *orig_ignore_func;
-  Function *orig_entry_func;
-  CPPFunction *orig_attempt_func;
+  rl_compignore_func_t *orig_ignore_func;
+  rl_compentry_func_t *orig_entry_func;
+  rl_quote_func_t *orig_quoting_func;
+  rl_completion_func_t *orig_attempt_func;
+  int orig_quoting_desired, r;
 
   orig_ignore_func = rl_ignore_some_completions_function;
   orig_attempt_func = rl_attempted_completion_function;
   orig_entry_func = rl_completion_entry_function;
+  orig_quoting_func = rl_filename_quoting_function;
+  orig_quoting_desired = rl_filename_quoting_desired;
 
-  rl_completion_entry_function = (Function *) filename_completion_function;
-  rl_attempted_completion_function = NULL;
-  rl_ignore_some_completions_function = (Function *) hack_braces_completion;
+  rl_completion_entry_function = rl_filename_completion_function;
+  rl_attempted_completion_function = (rl_completion_func_t *)NULL;
+  rl_ignore_some_completions_function = hack_braces_completion;
+  rl_filename_quoting_function = (rl_quote_func_t *)NULL;
+  rl_filename_quoting_desired = 0;
 
-  rl_complete_internal (TAB);
+  r = rl_complete_internal (TAB);
 
   rl_ignore_some_completions_function = orig_ignore_func;
   rl_attempted_completion_function = orig_attempt_func;
   rl_completion_entry_function = orig_entry_func;
+  rl_filename_quoting_function = orig_quoting_func;
+  rl_filename_quoting_desired = orig_quoting_desired;
+
+  return r;
 }
+#endif /* BRACE_EXPANSION && READLINE */