Imported from ../bash-3.2.48.tar.gz.
[platform/upstream/bash.git] / pathexp.c
index a7b69c1..69966ac 100644 (file)
--- a/pathexp.c
+++ b/pathexp.c
@@ -1,6 +1,6 @@
 /* pathexp.c -- The shell interface to the globbing library. */
 
-/* Copyright (C) 1995 Free Software Foundation, Inc.
+/* Copyright (C) 1995-2007 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -16,7 +16,7 @@
 
    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. */
+   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #include "config.h"
 
 #include "pathexp.h"
 #include "flags.h"
 
-#include <glob/fnmatch.h>
-#include <glob/glob.h>
+#include "shmbutil.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 *));
+               
+#if defined (USE_POSIX_GLOB_LIBRARY)
+#  include <glob.h>
+typedef int posix_glob_errfunc_t __P((const char *, int));
+#else
+#  include <glob/glob.h>
+#endif
 
 /* Control whether * matches .files in globbing. */
 int glob_dot_filenames;
@@ -48,9 +59,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)
@@ -80,6 +96,43 @@ 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);
 }
@@ -96,13 +149,13 @@ unquoted_glob_pattern_p (string)
    to match a filename should be performed. */
 char *
 quote_string_for_globbing (pathname, qflags)
-     char *pathname;
+     const char *pathname;
      int qflags;
 {
   char *temp;
   register int i, j;
 
-  temp = xmalloc (strlen (pathname) + 1);
+  temp = (char *)xmalloc (strlen (pathname) + 1);
 
   if ((qflags & QGLOB_CVTNULL) && QUOTED_NULL (pathname))
     {
@@ -113,13 +166,17 @@ quote_string_for_globbing (pathname, qflags)
   for (i = j = 0; pathname[i]; i++)
     {
       if (pathname[i] == CTLESC)
-        {
-          if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
-            continue;
+       {
+         if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
+           continue;
+         if ((qflags & QGLOB_REGEXP) && ere_char (pathname[i+1]) == 0)
+           continue;
          temp[j++] = '\\';
-        }
-      else
-        temp[j++] = pathname[i];
+         i++;
+         if (pathname[i] == '\0')
+           break;
+       }
+      temp[j++] = pathname[i];
     }
   temp[j] = '\0';
 
@@ -130,28 +187,36 @@ 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 = 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 '!':
+       {
+       case '*':
+       case '[':
+       case ']':
+       case '?':
+       case '\\':
+         *t++ = '\\';
+         break;
+       case '+':
+       case '@':
+       case '!':
          if (s[1] == '(')      /*(*/
            *t++ = '\\';
          break;
-        }
-      *t++ = *s++;
+       }
+
+      /* Copy a single (possibly multibyte) character from s to t,
+         incrementing both. */
+      COPY_CHAR_P (t, s, send);
     }
   *t = '\0';
   return temp;
@@ -160,11 +225,11 @@ quote_globbing_chars (string)
 /* Call the glob library to do globbing on PATHNAME. */
 char **
 shell_glob_filename (pathname)
-     char *pathname;
+     const char *pathname;
 {
 #if defined (USE_POSIX_GLOB_LIBRARY)
   register int i;
-  char *temp, **return_value;
+  char *temp, **results;
   glob_t filenames;
   int glob_flags;
 
@@ -172,20 +237,41 @@ shell_glob_filename (pathname)
 
   filenames.gl_offs = 0;
 
+#  if defined (GLOB_PERIOD)
   glob_flags = glob_dot_filenames ? GLOB_PERIOD : 0;
+#  else
+  glob_flags = 0;
+#  endif /* !GLOB_PERIOD */
+
   glob_flags |= (GLOB_ERR | GLOB_DOOFFS);
 
-  i = glob (temp, glob_flags, (Function *)NULL, &filenames);
+  i = glob (temp, glob_flags, (posix_glob_errfunc_t *)NULL, &filenames);
 
   free (temp);
 
-  if (i == GLOB_NOSPACE || i == GLOB_ABEND)
+  if (i == GLOB_NOSPACE || i == GLOB_ABORTED)
     return ((char **)NULL);
-
-  if (i == GLOB_NOMATCH)
+  else if (i == GLOB_NOMATCH)
+    filenames.gl_pathv = (char **)NULL;
+  else if (i != 0)             /* other error codes not in POSIX.2 */
     filenames.gl_pathv = (char **)NULL;
 
-  return (filenames.gl_pathv);
+  results = filenames.gl_pathv;
+
+  if (results && ((GLOB_FAILED (results)) == 0))
+    {
+      if (should_ignore_glob_matches ())
+       ignore_glob_matches (results);
+      if (results && results[0])
+       strvec_sort (results);
+      else
+       {
+         FREE (results);
+         results = (char **)NULL;
+       }
+    }
+
+  return (results);
 
 #else /* !USE_POSIX_GLOB_LIBRARY */
 
@@ -194,8 +280,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, 0);
   free (temp);
 
   if (results && ((GLOB_FAILED (results)) == 0))
@@ -203,7 +288,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);
@@ -223,7 +308,7 @@ static struct ignorevar globignore =
   (struct ign *)0,
   0,
   (char *)0,
-  (Function *)0,
+  (sh_iv_item_func_t *)0,
 };
 
 /* Set up to ignore some glob matches because the value of GLOBIGNORE
@@ -253,7 +338,7 @@ should_ignore_glob_matches ()
 /* Return 0 if NAME matches a pattern in the globignore.ignores list. */
 static int
 glob_name_is_acceptable (name)
-     char *name;
+     const char *name;
 {
   struct ign *p;
   int flags;
@@ -265,8 +350,8 @@ glob_name_is_acceptable (name)
   flags = FNM_PATHNAME | FNMATCH_EXTFLAG;
   for (p = globignore.ignores; p->val; p++)
     {
-      if (fnmatch (p->val, name, flags) != FNM_NOMATCH)
-        return (0);
+      if (strmatch (p->val, (char *)name, flags) != FNM_NOMATCH)
+       return (0);
     }
   return (1);
 }
@@ -280,19 +365,19 @@ glob_name_is_acceptable (name)
 static void
 ignore_globbed_names (names, name_func)
      char **names;
-     Function *name_func;
+     sh_ignore_func_t *name_func;
 {
   char **newnames;
   int n, i;
 
   for (i = 0; names[i]; i++)
     ;
-  newnames = (char **)xmalloc ((i + 1) * sizeof (char *));
+  newnames = strvec_create (i + 1);
 
   for (n = i = 0; names[i]; i++)
     {
       if ((*name_func) (names[i]))
-        newnames[n++] = names[i];
+       newnames[n++] = names[i];
       else
        free (names[i]);
     }
@@ -374,7 +459,7 @@ setup_ignore_patterns (ivp)
       ivp->ignores[numitems].len = strlen (colon_bit);
       ivp->ignores[numitems].flags = 0;
       if (ivp->item_func)
-        (*ivp->item_func) (&ivp->ignores[numitems]);
+       (*ivp->item_func) (&ivp->ignores[numitems]);
       numitems++;
     }
   ivp->ignores[numitems].val = (char *)NULL;