]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - pathexp.c
Bash-4.2 patch 27
[thirdparty/bash.git] / pathexp.c
index aae09a7051fead8bac8d014f4e3a30814935a583..f23995684acbe42a25af6bb4e7e68594890edc11 100644 (file)
--- a/pathexp.c
+++ b/pathexp.c
@@ -1,22 +1,22 @@
 /* pathexp.c -- The shell interface to the globbing library. */
 
-/* Copyright (C) 1995 Free Software Foundation, Inc.
+/* Copyright (C) 1995-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, 59 Temple Place, Suite 330, Boston, MA 02111 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"
 
 #include "pathexp.h"
 #include "flags.h"
 
+#include "shmbutil.h"
+#include "bashintl.h"
+
 #include <glob/strmatch.h>
 
+static int glob_name_is_acceptable __P((const char *));
+static void ignore_globbed_names __P((char **, sh_ignore_func_t *));
+static char *split_ignorespec __P((char *, int *));
+               
 #if defined (USE_POSIX_GLOB_LIBRARY)
 #  include <glob.h>
 typedef int posix_glob_errfunc_t __P((const char *, int));
@@ -46,7 +53,10 @@ typedef int posix_glob_errfunc_t __P((const char *, int));
 int glob_dot_filenames;
 
 /* Control whether the extended globbing features are enabled. */
-int extended_glob = 0;
+int extended_glob = EXTGLOB_DEFAULT;
+
+/* Control enabling special handling of `**' */
+int glob_star = 0;
 
 /* Return nonzero if STRING has any unquoted special globbing chars in it.  */
 int
@@ -54,9 +64,14 @@ unquoted_glob_pattern_p (string)
      register char *string;
 {
   register int c;
+  char *send;
   int open;
 
+  DECLARE_MBSTATE;
+
   open = 0;
+  send = string + strlen (string);
+
   while (c = *string++)
     {
       switch (c)
@@ -86,10 +101,69 @@ unquoted_glob_pattern_p (string)
          if (*string++ == '\0')
            return (0);
        }
+
+      /* Advance one fewer byte than an entire multibyte character to
+        account for the auto-increment in the loop above. */
+#ifdef HANDLE_MULTIBYTE
+      string--;
+      ADVANCE_CHAR_P (string, send - string);
+      string++;
+#else
+      ADVANCE_CHAR_P (string, send - string);
+#endif
     }
   return (0);
 }
 
+/* Return 1 if C is a character that is `special' in a POSIX ERE and needs to
+   be quoted to match itself. */
+static inline int
+ere_char (c)
+     int c;
+{
+  switch (c)
+    {
+    case '.':
+    case '[':
+    case '\\':
+    case '(':
+    case ')':
+    case '*':
+    case '+':
+    case '?':
+    case '{':
+    case '|':
+    case '^':
+    case '$':
+      return 1;
+    default: 
+      return 0;
+    }
+  return (0);
+}
+
+int
+glob_char_p (s)
+     const char *s;
+{
+  switch (*s)
+    {
+    case '*':
+    case '[':
+    case ']':
+    case '?':
+    case '\\':
+      return 1;
+    case '+':
+    case '@':
+    case '!':
+      if (s[1] == '(')        /*(*/
+       return 1;
+      break;
+    }
+  return 0;
+}
+
 /* PATHNAME can contain characters prefixed by CTLESC; this indicates
    that the character is to be quoted.  We quote it here in the style
    that the glob library recognizes.  If flags includes QGLOB_CVTNULL,
@@ -122,10 +196,21 @@ quote_string_for_globbing (pathname, qflags)
        {
          if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
            continue;
+         if (pathname[i+1] != CTLESC && (qflags & QGLOB_REGEXP) && ere_char (pathname[i+1]) == 0)
+           continue;
          temp[j++] = '\\';
+         i++;
+         if (pathname[i] == '\0')
+           break;
        }
-      else
-       temp[j++] = pathname[i];
+      else if (pathname[i] == '\\')
+       {
+         temp[j++] = '\\';
+         i++;
+         if (pathname[i] == '\0')
+           break;
+       }
+      temp[j++] = pathname[i];
     }
   temp[j] = '\0';
 
@@ -136,28 +221,22 @@ char *
 quote_globbing_chars (string)
      char *string;
 {
-  char *temp, *s, *t;
+  size_t slen;
+  char *temp, *s, *t, *send;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  send = string + slen;
 
-  temp = (char *)xmalloc (strlen (string) * 2 + 1);
+  temp = (char *)xmalloc (slen * 2 + 1);
   for (t = temp, s = string; *s; )
     {
-      switch (*s)
-       {
-       case '*':
-       case '[':
-       case ']':
-       case '?':
-       case '\\':
-         *t++ = '\\';
-         break;
-       case '+':
-       case '@':
-       case '!':
-         if (s[1] == '(')      /*(*/
-           *t++ = '\\';
-         break;
-       }
-      *t++ = *s++;
+      if (glob_char_p (s))
+       *t++ = '\\';
+
+      /* Copy a single (possibly multibyte) character from s to t,
+         incrementing both. */
+      COPY_CHAR_P (t, s, send);
     }
   *t = '\0';
   return temp;
@@ -204,7 +283,7 @@ shell_glob_filename (pathname)
       if (should_ignore_glob_matches ())
        ignore_glob_matches (results);
       if (results && results[0])
-       sort_char_array (results);
+       strvec_sort (results);
       else
        {
          FREE (results);
@@ -221,8 +300,7 @@ shell_glob_filename (pathname)
   noglob_dot_filenames = glob_dot_filenames == 0;
 
   temp = quote_string_for_globbing (pathname, QGLOB_FILENAME);
-
-  results = glob_filename (temp);
+  results = glob_filename (temp, glob_star ? GX_GLOBSTAR : 0);
   free (temp);
 
   if (results && ((GLOB_FAILED (results)) == 0))
@@ -230,7 +308,7 @@ shell_glob_filename (pathname)
       if (should_ignore_glob_matches ())
        ignore_glob_matches (results);
       if (results && results[0])
-       sort_char_array (results);
+       strvec_sort (results);
       else
        {
          FREE (results);
@@ -314,7 +392,7 @@ ignore_globbed_names (names, name_func)
 
   for (i = 0; names[i]; i++)
     ;
-  newnames = alloc_array (i + 1);
+  newnames = strvec_create (i + 1);
 
   for (n = i = 0; names[i]; i++)
     {
@@ -351,6 +429,30 @@ ignore_glob_matches (names)
   ignore_globbed_names (names, glob_name_is_acceptable);
 }
 
+static char *
+split_ignorespec (s, ip)
+     char *s;
+     int *ip;
+{
+  char *t;
+  int n, i;
+
+  if (s == 0)
+    return 0;
+
+  i = *ip;
+  if (s[i] == 0)
+    return 0;
+
+  n = skip_to_delim (s, i, ":", SD_NOJMP|SD_EXTGLOB);
+  t = substring (s, i, n);
+
+  if (s[n] == ':')
+    n++;  
+  *ip = n;  
+  return t;
+}
+  
 void
 setup_ignore_patterns (ivp)
      struct ignorevar *ivp;
@@ -390,7 +492,11 @@ setup_ignore_patterns (ivp)
 
   numitems = maxitems = ptr = 0;
 
+#if 0
   while (colon_bit = extract_colon_unit (this_ignoreval, &ptr))
+#else
+  while (colon_bit = split_ignorespec (this_ignoreval, &ptr))
+#endif
     {
       if (numitems + 1 >= maxitems)
        {