GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
getopt-wrapper.c
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2016-2017 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 // getopt_long may be provided by gnulib. We don't include gnulib
24 // headers directly in Octave's C++ source files to avoid problems that
25 // may be caused by the way that gnulib overrides standard library
26 // functions.
27 
28 #if defined (HAVE_CONFIG_H)
29 # include "config.h"
30 #endif
31 
32 #include <stdlib.h>
33 
34 #include <getopt.h>
35 
36 #include "getopt-wrapper.h"
37 
38 static struct option *
40 {
41  const struct octave_getopt_options *p = opts;
42  struct option *retval = 0, *q = 0;
43 
44  int n = 0;
45  while (p->name)
46  {
47  n++;
48  p++;
49  }
50 
51  retval = (struct option *) malloc ((n+1) * sizeof (struct option));
52 
53  p = opts;
54  q = retval;
55  while (p->name)
56  {
57  q->name = p->name;
58 
59  switch (p->has_arg)
60  {
62  q->has_arg = required_argument;
63  break;
64 
66  q->has_arg = optional_argument;
67  break;
68 
69  default:
70  q->has_arg = no_argument;
71  break;
72  }
73 
74  q->flag = p->flag;
75 
76  q->val = p->val;
77 
78  q++;
79  p++;
80  }
81 
82  q->name = 0;
83  q->has_arg = 0;
84  q->flag = 0;
85  q->val = 0;
86 
87  return retval;
88 }
89 
90 int
92  const char *shortopts,
93  const struct octave_getopt_options *longopts,
94  int *longind)
95 {
96  struct option *lopts = make_option_struct (longopts);
97 
98  int retval = getopt_long (argc, argv, shortopts, lopts, longind);
99 
100  free (lopts);
101 
102  return retval;
103 }
104 
105 char *
107 {
108  return optarg;
109 }
110 
111 int
113 {
114  return optind;
115 }
option
Definition: sighandlers.cc:767
#define octave_required_arg
int argc
Definition: load-save.cc:633
string_vector argv
Definition: load-save.cc:635
#define octave_optional_arg
octave_value retval
Definition: data.cc:6294
char * octave_optarg_wrapper(void)
int octave_getopt_long_wrapper(int argc, char **argv, const char *shortopts, const struct octave_getopt_options *longopts, int *longind)
static struct option * make_option_struct(const struct octave_getopt_options *opts)
void free(void *)
p
Definition: lu.cc:138
int octave_optind_wrapper(void)
void * malloc(size_t)