GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
getopt-wrapper.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // getopt_long may be provided by gnulib. We don't include gnulib
27 // headers directly in Octave's C++ source files to avoid problems that
28 // may be caused by the way that gnulib overrides standard library
29 // functions.
30 
31 #if defined (HAVE_CONFIG_H)
32 # include "config.h"
33 #endif
34 
35 #include <stdlib.h>
36 
37 #include <getopt.h>
38 
39 #include "getopt-wrapper.h"
40 
41 static struct option *
42 make_option_struct (const struct octave_getopt_options *opts)
43 {
44  const struct octave_getopt_options *p = opts;
45  struct option *retval = 0, *q = 0;
46 
47  int n = 0;
48  while (p->name)
49  {
50  n++;
51  p++;
52  }
53 
54  retval = (struct option *) malloc ((n+1) * sizeof (struct option));
55 
56  // If we don't have enough memory even to start Octave
57  // then we might as well quit now.
58  if (! retval)
59  abort ();
60 
61  p = opts;
62  q = retval;
63  while (p->name)
64  {
65  q->name = p->name;
66 
67  switch (p->has_arg)
68  {
70  q->has_arg = required_argument;
71  break;
72 
74  q->has_arg = optional_argument;
75  break;
76 
77  default:
78  q->has_arg = no_argument;
79  break;
80  }
81 
82  q->flag = p->flag;
83 
84  q->val = p->val;
85 
86  q++;
87  p++;
88  }
89 
90  q->name = 0;
91  q->has_arg = 0;
92  q->flag = 0;
93  q->val = 0;
94 
95  return retval;
96 }
97 
98 int
99 octave_getopt_long_wrapper (int argc, char **argv,
100  const char *shortopts,
101  const struct octave_getopt_options *longopts,
102  int *longind)
103 {
104  struct option *lopts = make_option_struct (longopts);
105 
106  int retval = getopt_long (argc, argv, shortopts, lopts, longind);
107 
108  free (lopts);
109 
110  return retval;
111 }
112 
113 char *
115 {
116  return optarg;
117 }
118 
119 int
121 {
122  return optind;
123 }
124 
125 int
127 {
128  return opterr;
129 }
130 
131 int
133 {
134  int retval = opterr;
135 
136  opterr = val;
137 
138  return retval;
139 }
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)
int octave_get_opterr_wrapper(void)
int octave_optind_wrapper(void)
int octave_set_opterr_wrapper(int val)
#define octave_required_arg
#define octave_optional_arg
octave_idx_type n
Definition: mx-inlines.cc:761
void * malloc(unsigned)
void free(void *)