Imported from ../bash-3.2.48.tar.gz.
[platform/upstream/bash.git] / pathexp.c
index 90ef72d..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.
 
 #include "pathexp.h"
 #include "flags.h"
 
-#include <glob/fnmatch.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
@@ -53,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)
@@ -85,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);
 }
@@ -107,7 +155,7 @@ quote_string_for_globbing (pathname, 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))
     {
@@ -121,10 +169,14 @@ quote_string_for_globbing (pathname, qflags)
        {
          if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
            continue;
+         if ((qflags & QGLOB_REGEXP) && ere_char (pathname[i+1]) == 0)
+           continue;
          temp[j++] = '\\';
+         i++;
+         if (pathname[i] == '\0')
+           break;
        }
-      else
-       temp[j++] = pathname[i];
+      temp[j++] = pathname[i];
     }
   temp[j] = '\0';
 
@@ -135,9 +187,14 @@ char *
 quote_globbing_chars (string)
      char *string;
 {
-  char *temp, *s, *t;
+  size_t slen;
+  char *temp, *s, *t, *send;
+  DECLARE_MBSTATE;
 
-  temp = xmalloc (strlen (string) * 2 + 1);
+  slen = strlen (string);
+  send = string + slen;
+
+  temp = (char *)xmalloc (slen * 2 + 1);
   for (t = temp, s = string; *s; )
     {
       switch (*s)
@@ -156,7 +213,10 @@ quote_globbing_chars (string)
            *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;
@@ -185,7 +245,7 @@ shell_glob_filename (pathname)
 
   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);
 
@@ -203,7 +263,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);
@@ -220,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))
@@ -229,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);
@@ -249,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
@@ -279,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;
@@ -291,7 +350,7 @@ 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)
+      if (strmatch (p->val, (char *)name, flags) != FNM_NOMATCH)
        return (0);
     }
   return (1);
@@ -306,14 +365,14 @@ 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 = alloc_array (i + 1);
+  newnames = strvec_create (i + 1);
 
   for (n = i = 0; names[i]; i++)
     {