changeset 21929:7ab7cd327257

hide fnmatch.h and glob.h headers * liboctave/wrappers/glob-wrappers.c, liboctave/wrappers/glob-wrappers.h: New files. * liboctave/wrappers/module.mk: Update. * glob-match.cc, oct-glob.cc: Use new wrapper functions.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Jun 2016 13:09:54 -0400
parents 315f4ba604c8
children f0c5dd1ea2b9
files liboctave/util/glob-match.cc liboctave/util/oct-glob.cc liboctave/wrappers/glob-wrappers.c liboctave/wrappers/glob-wrappers.h liboctave/wrappers/module.mk
diffstat 5 files changed, 197 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/util/glob-match.cc	Thu Jun 16 13:07:34 2016 -0400
+++ b/liboctave/util/glob-match.cc	Thu Jun 16 13:09:54 2016 -0400
@@ -24,10 +24,9 @@
 #  include "config.h"
 #endif
 
-#include <fnmatch.h>
-
 #include "glob-match.h"
 #include "oct-glob.h"
+#include "glob-wrappers.h"
 
 bool
 glob_match::match (const std::string& str) const
@@ -47,13 +46,13 @@
   int retval = 0;
 
   if (xopts & pathname)
-    retval |= FNM_PATHNAME;
+    retval |= octave_fnm_pathname_wrapper ();
 
   if (xopts & noescape)
-    retval |= FNM_NOESCAPE;
+    retval |= octave_fnm_noescape_wrapper ();
 
   if (xopts & period)
-    retval |= FNM_PERIOD;
+    retval |= octave_fnm_period_wrapper ();
 
   return retval;
 }
--- a/liboctave/util/oct-glob.cc	Thu Jun 16 13:07:34 2016 -0400
+++ b/liboctave/util/oct-glob.cc	Thu Jun 16 13:09:54 2016 -0400
@@ -27,11 +27,11 @@
 #include <algorithm>
 #include <string>
 
-#include <fnmatch.h>
-#include <glob.h>
+#include "glob-wrappers.h"
 
 #include "oct-glob.h"
 #include "file-stat.h"
+#include "unwind-prot.h"
 
 // These functions are defined here and not in glob_match.cc so that we
 // can include the glob.h file from gnulib, which defines glob to
@@ -59,7 +59,8 @@
       const char *cstr = str.c_str ();
 
       for (int i = 0; i < npat; i++)
-        if (::fnmatch (pat(i).c_str (), cstr, fnm_flags) != FNM_NOMATCH)
+        if (octave_fnmatch_wrapper (pat(i).c_str (), cstr, fnm_flags)
+            != octave_fnm_nomatch_wrapper ())
           return true;
 
       return false;
@@ -74,14 +75,18 @@
 
       int k = 0;
 
+      octave::unwind_protect frame;
+
+      void *glob_info = octave_create_glob_info_struct ();
+
+      frame.add_fcn (octave_destroy_glob_info_struct, glob_info);
+
       for (int i = 0; i < npat; i++)
         {
           std::string xpat = pat(i);
 
           if (! xpat.empty ())
             {
-              glob_t glob_info;
-
 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
      && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
               std::replace_if (xpat.begin (), xpat.end (),
@@ -89,13 +94,16 @@
                                '/');
 #endif
 
-              int err = gnulib::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info);
+              int err = octave_glob_wrapper (xpat.c_str (),
+                                             octave_glob_nosort_wrapper (),
+                                             glob_info);
 
               if (! err)
                 {
-                  int n = glob_info.gl_pathc;
+                  int n = octave_glob_num_matches (glob_info);
 
-                  const char * const *matches = glob_info.gl_pathv;
+                  const char * const *matches
+                    = octave_glob_match_list (glob_info);
 
                   // FIXME: we shouldn't have to check to see if
                   // a single match exists, but it seems that glob() won't
@@ -124,7 +132,7 @@
                         }
                     }
 
-                  gnulib::globfree (&glob_info);
+                  octave_globfree_wrapper (glob_info);
                 }
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/glob-wrappers.c	Thu Jun 16 13:09:54 2016 -0400
@@ -0,0 +1,110 @@
+/*
+
+Copyright (C) 2016 John W. Eaton
+
+This file is part of Octave.
+
+Octave 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.
+
+Octave 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 Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+// These functions may be provided by gnulib.  We don't include gnulib
+// headers directly in Octave's C++ source files to avoid problems that
+// may be caused by the way that gnulib overrides standard library
+// functions.
+
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include <stdlib.h>
+
+#include <fnmatch.h>
+#include <glob.h>
+
+#include "glob-wrappers.h"
+
+void *
+octave_create_glob_info_struct (void)
+{
+  return malloc (sizeof (glob_t));
+}
+
+// Does not call globfree.
+void
+octave_destroy_glob_info_struct (void *glob_info)
+{
+  free (glob_info);
+}
+
+int
+octave_glob_wrapper (const char *pattern, int flags, void *glob_info)
+{
+  return glob (pattern, flags, 0, glob_info);
+}
+
+int
+octave_glob_num_matches (void *glob_info)
+{
+  return glob_info ? ((glob_t *) glob_info)->gl_pathc : 0;
+}
+
+const char * const *
+octave_glob_match_list (void *glob_info)
+{
+  return glob_info ? ((glob_t *) glob_info)->gl_pathv : 0;
+}
+
+void
+octave_globfree_wrapper (void *glob_info)
+{
+  globfree ((glob_t *) glob_info);
+}
+
+int
+octave_glob_nosort_wrapper (void)
+{
+  return GLOB_NOSORT;
+}
+
+int
+octave_fnmatch_wrapper (const char *pattern, const char *name, int flags)
+{
+  return fnmatch (pattern, name, flags);
+}
+
+int
+octave_fnm_nomatch_wrapper (void)
+{
+  return FNM_NOMATCH;
+}
+
+int
+octave_fnm_pathname_wrapper (void)
+{
+  return FNM_PATHNAME;
+}
+
+int
+octave_fnm_noescape_wrapper (void)
+{
+  return FNM_NOESCAPE;
+}
+
+int
+octave_fnm_period_wrapper (void)
+{
+  return FNM_PERIOD;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/glob-wrappers.h	Thu Jun 16 13:09:54 2016 -0400
@@ -0,0 +1,64 @@
+/*
+
+Copyright (C) 2016 John W. Eaton
+
+This file is part of Octave.
+
+Octave 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.
+
+Octave 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 Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (octave_glob_wrappers_h)
+#define octave_glob_wrappers_h 1
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+extern void *octave_create_glob_info_struct (void);
+
+// Does not call globfree.
+extern void octave_destroy_glob_info_struct (void *glob_info);
+
+// We don't need the error function pointer that the system glob
+// function allows.
+extern int
+octave_glob_wrapper (const char *pattern, int flags, void *glob_info);
+
+extern int octave_glob_num_matches (void *glob_info);
+
+extern const char * const *octave_glob_match_list (void *glob_info);
+
+extern void octave_globfree_wrapper (void *glob_info);
+
+extern int octave_glob_nosort_wrapper (void);
+  
+extern int
+octave_fnmatch_wrapper (const char *pattern, const char *name, int flags);
+
+extern int octave_fnm_nomatch_wrapper (void);
+
+extern int octave_fnm_pathname_wrapper (void);
+
+extern int octave_fnm_noescape_wrapper (void);
+
+extern int octave_fnm_period_wrapper (void);
+
+#if defined __cplusplus
+}
+#endif
+
+#endif
+
--- a/liboctave/wrappers/module.mk	Thu Jun 16 13:07:34 2016 -0400
+++ b/liboctave/wrappers/module.mk	Thu Jun 16 13:09:54 2016 -0400
@@ -7,6 +7,7 @@
   liboctave/wrappers/filepos-wrappers.h \
   liboctave/wrappers/fpucw-wrapper.h \
   liboctave/wrappers/gen-tempname-wrapper.h \
+  liboctave/wrappers/glob-wrappers.h \
   liboctave/wrappers/hash-wrappers.h \
   liboctave/wrappers/mkostemp-wrapper.h \
   liboctave/wrappers/nanosleep-wrapper.h \
@@ -34,6 +35,7 @@
   liboctave/wrappers/filepos-wrappers.c \
   liboctave/wrappers/fpucw-wrapper.c \
   liboctave/wrappers/gen-tempname-wrapper.c \
+  liboctave/wrappers/glob-wrappers.c \
   liboctave/wrappers/hash-wrappers.c \
   liboctave/wrappers/mkostemp-wrapper.c \
   liboctave/wrappers/nanosleep-wrapper.c \