changeset 22064:bd4f3d2ce52f

provide wrapper for getopt * liboctave/wrappers/getopt-wrapper.c, liboctave/wrappers/getopt-wrapper.h: New files. * liboctave/wrappers/module.mk: Update. * octave.cc, options-usage.h: Use wrapper for getopt.
author John W. Eaton <jwe@octave.org>
date Thu, 07 Jul 2016 13:44:40 -0400
parents 55c20a3e3e59
children cf1bda5ca9c9
files libinterp/octave.cc libinterp/options-usage.h liboctave/wrappers/getopt-wrapper.c liboctave/wrappers/getopt-wrapper.h liboctave/wrappers/module.mk
diffstat 5 files changed, 195 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave.cc	Thu Jul 07 17:22:25 2016 +0100
+++ b/libinterp/octave.cc	Thu Jul 07 13:44:40 2016 -0400
@@ -34,12 +34,11 @@
 
 #include <iostream>
 
-#include <getopt.h>
-
 #include "cmd-edit.h"
 #include "f77-fcn.h"
 #include "file-ops.h"
 #include "file-stat.h"
+#include "getopt-wrapper.h"
 #include "lo-error.h"
 #include "oct-env.h"
 #include "str-vec.h"
@@ -587,7 +586,8 @@
     {
       int long_idx;
 
-      int optc = getopt_long (argc, argv, short_opts, long_opts, &long_idx);
+      int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
+                                             long_opts, &long_idx);
 
       if (optc < 0)
         break;
--- a/libinterp/options-usage.h	Thu Jul 07 17:22:25 2016 +0100
+++ b/libinterp/options-usage.h	Thu Jul 07 13:44:40 2016 -0400
@@ -67,41 +67,41 @@
 #define PERSIST_OPTION 17
 #define TEXI_MACROS_FILE_OPTION 18
 #define TRADITIONAL_OPTION 19
-struct option long_opts[] =
+struct octave_getopt_options long_opts[] =
 {
-  { "braindead",                no_argument,       0, TRADITIONAL_OPTION },
-  { "built-in-docstrings-file", required_argument, 0, BUILT_IN_DOCSTRINGS_FILE_OPTION },
-  { "debug",                    no_argument,       0, 'd' },
-  { "debug-jit",                no_argument,       0, DEBUG_JIT_OPTION },
-  { "doc-cache-file",           required_argument, 0, DOC_CACHE_FILE_OPTION },
-  { "echo-commands",            no_argument,       0, 'x' },
-  { "eval",                     required_argument, 0, EVAL_OPTION },
-  { "exec-path",                required_argument, 0, EXEC_PATH_OPTION },
-  { "force-gui",                no_argument,       0, FORCE_GUI_OPTION },
-  { "help",                     no_argument,       0, 'h' },
-  { "image-path",               required_argument, 0, IMAGE_PATH_OPTION },
-  { "info-file",                required_argument, 0, INFO_FILE_OPTION },
-  { "info-program",             required_argument, 0, INFO_PROG_OPTION },
-  { "interactive",              no_argument,       0, 'i' },
-  { "jit-compiler",             no_argument,       0, JIT_COMPILER_OPTION },
-  { "line-editing",             no_argument,       0, LINE_EDITING_OPTION },
-  { "no-gui",                   no_argument,       0, NO_GUI_OPTION },
-  { "no-history",               no_argument,       0, 'H' },
-  { "no-init-file",             no_argument,       0, NO_INIT_FILE_OPTION },
-  { "no-init-path",             no_argument,       0, NO_INIT_PATH_OPTION },
-  { "no-line-editing",          no_argument,       0, NO_LINE_EDITING_OPTION },
-  { "no-site-file",             no_argument,       0, NO_SITE_FILE_OPTION },
-  { "no-window-system",         no_argument,       0, 'W' },
-  { "norc",                     no_argument,       0, 'f' },
-  { "path",                     required_argument, 0, 'p' },
-  { "persist",                  no_argument,       0, PERSIST_OPTION },
-  { "quiet",                    no_argument,       0, 'q' },
-  { "silent",                   no_argument,       0, 'q' },
-  { "texi-macros-file",         required_argument, 0, TEXI_MACROS_FILE_OPTION },
-  { "traditional",              no_argument,       0, TRADITIONAL_OPTION },
-  { "verbose",                  no_argument,       0, 'V' },
-  { "version",                  no_argument,       0, 'v' },
-  { 0,                          0,                 0, 0 }
+  { "braindead",                octave_no_arg,       0, TRADITIONAL_OPTION },
+  { "built-in-docstrings-file", octave_required_arg, 0, BUILT_IN_DOCSTRINGS_FILE_OPTION },
+  { "debug",                    octave_no_arg,       0, 'd' },
+  { "debug-jit",                octave_no_arg,       0, DEBUG_JIT_OPTION },
+  { "doc-cache-file",           octave_required_arg, 0, DOC_CACHE_FILE_OPTION },
+  { "echo-commands",            octave_no_arg,       0, 'x' },
+  { "eval",                     octave_required_arg, 0, EVAL_OPTION },
+  { "exec-path",                octave_required_arg, 0, EXEC_PATH_OPTION },
+  { "force-gui",                octave_no_arg,       0, FORCE_GUI_OPTION },
+  { "help",                     octave_no_arg,       0, 'h' },
+  { "image-path",               octave_required_arg, 0, IMAGE_PATH_OPTION },
+  { "info-file",                octave_required_arg, 0, INFO_FILE_OPTION },
+  { "info-program",             octave_required_arg, 0, INFO_PROG_OPTION },
+  { "interactive",              octave_no_arg,       0, 'i' },
+  { "jit-compiler",             octave_no_arg,       0, JIT_COMPILER_OPTION },
+  { "line-editing",             octave_no_arg,       0, LINE_EDITING_OPTION },
+  { "no-gui",                   octave_no_arg,       0, NO_GUI_OPTION },
+  { "no-history",               octave_no_arg,       0, 'H' },
+  { "no-init-file",             octave_no_arg,       0, NO_INIT_FILE_OPTION },
+  { "no-init-path",             octave_no_arg,       0, NO_INIT_PATH_OPTION },
+  { "no-line-editing",          octave_no_arg,       0, NO_LINE_EDITING_OPTION },
+  { "no-site-file",             octave_no_arg,       0, NO_SITE_FILE_OPTION },
+  { "no-window-system",         octave_no_arg,       0, 'W' },
+  { "norc",                     octave_no_arg,       0, 'f' },
+  { "path",                     octave_required_arg, 0, 'p' },
+  { "persist",                  octave_no_arg,       0, PERSIST_OPTION },
+  { "quiet",                    octave_no_arg,       0, 'q' },
+  { "silent",                   octave_no_arg,       0, 'q' },
+  { "texi-macros-file",         octave_required_arg, 0, TEXI_MACROS_FILE_OPTION },
+  { "traditional",              octave_no_arg,       0, TRADITIONAL_OPTION },
+  { "verbose",                  octave_no_arg,       0, 'V' },
+  { "version",                  octave_no_arg,       0, 'v' },
+  { 0,                          0,                   0, 0 }
 };
 
 // Usage message with extra help.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/getopt-wrapper.c	Thu Jul 07 13:44:40 2016 -0400
@@ -0,0 +1,103 @@
+/*
+
+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/>.
+
+*/
+
+// getopt_long 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 <getopt.h>
+
+#include "getopt-wrapper.h"
+
+static struct option *
+make_option_struct (const struct octave_getopt_options *opts)
+{
+  const struct octave_getopt_options *p = opts;
+  struct option *retval = 0, *q = 0;
+
+  int n = 0;
+  while (p->name)
+    {
+      n++;
+      p++;
+    }
+
+  retval = (struct option *) malloc ((n+1) * sizeof (struct option));
+
+  p = opts;
+  q = retval;
+  while (p->name)
+    {
+      q->name = p->name;
+
+      switch (p->has_arg)
+        {
+        case octave_required_arg:
+          q->has_arg = required_argument;
+          break;
+
+        case octave_optional_arg:
+          q->has_arg = optional_argument;
+          break;
+
+        default:
+          q->has_arg = no_argument;
+          break;
+        }
+
+      q->flag = p->flag;
+
+      q->val = p->val;
+
+      q++;
+      p++;
+    }
+
+  q->name = 0;
+  q->has_arg = 0;
+  q->flag = 0;
+  q->val = 0;
+
+  return retval;
+}
+
+int
+octave_getopt_long_wrapper (int argc, char *const *argv,
+                            const char *shortopts,
+                            const struct octave_getopt_options *longopts,
+                            int *longind)
+{
+  struct option *lopts = make_option_struct (longopts);
+
+  int retval = getopt_long (argc, argv, shortopts, lopts, longind);
+
+  free (lopts);
+
+  return retval;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/getopt-wrapper.h	Thu Jul 07 13:44:40 2016 -0400
@@ -0,0 +1,53 @@
+/*
+
+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_getopt_wrapper_h)
+#define octave_getopt_wrapper_h 1
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+struct octave_getopt_options
+{
+  const char *name;
+  int has_arg;
+  int *flag;
+  int val;
+};
+
+#define octave_no_arg 0
+#define octave_required_arg 1
+#define octave_optional_arg 2
+
+extern int
+octave_getopt_long_wrapper (int argc, char *const *argv,
+                            const char *shortopts,
+                            const struct octave_getopt_options *longopts,
+                            int *longind);
+
+#if defined __cplusplus
+}
+#endif
+
+#endif
+
--- a/liboctave/wrappers/module.mk	Thu Jul 07 17:22:25 2016 +0100
+++ b/liboctave/wrappers/module.mk	Thu Jul 07 13:44:40 2016 -0400
@@ -7,6 +7,7 @@
   liboctave/wrappers/filepos-wrappers.h \
   liboctave/wrappers/fpucw-wrapper.h \
   liboctave/wrappers/gen-tempname-wrapper.h \
+  liboctave/wrappers/getopt-wrapper.h \
   liboctave/wrappers/glob-wrappers.h \
   liboctave/wrappers/hash-wrappers.h \
   liboctave/wrappers/mkostemp-wrapper.h \
@@ -39,6 +40,7 @@
   liboctave/wrappers/filepos-wrappers.c \
   liboctave/wrappers/fpucw-wrapper.c \
   liboctave/wrappers/gen-tempname-wrapper.c \
+  liboctave/wrappers/getopt-wrapper.c \
   liboctave/wrappers/glob-wrappers.c \
   liboctave/wrappers/hash-wrappers.c \
   liboctave/wrappers/mkostemp-wrapper.c \