annotate liboctave/wrappers/getopt-wrapper.c @ 29910:0f41e117789f stable

disable getopt error reporting in wrapper program (bug #60886) * getopt-wrapper.h, getopt-wrapper.c (octave_get_opterr_wrapper, octave_set_opterr_wrapper): New functions. * main.in.cc (main): Call octave_set_opterr_wrapper to disable getopt error reporting.
author John W. Eaton <jwe@octave.org>
date Thu, 22 Jul 2021 13:52:49 -0400
parents 0a5b15007766
children 796f54d4ddbf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
3 // Copyright (C) 2016-2021 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
22064
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 // getopt_long may be provided by gnulib. We don't include gnulib
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 // headers directly in Octave's C++ source files to avoid problems that
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 // may be caused by the way that gnulib overrides standard library
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 // functions.
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 #if defined (HAVE_CONFIG_H)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 # include "config.h"
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #endif
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 #include <stdlib.h>
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 #include <getopt.h>
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 #include "getopt-wrapper.h"
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 static struct option *
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 make_option_struct (const struct octave_getopt_options *opts)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 {
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 const struct octave_getopt_options *p = opts;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 struct option *retval = 0, *q = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 int n = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 while (p->name)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 {
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 n++;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 p++;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 }
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 retval = (struct option *) malloc ((n+1) * sizeof (struct option));
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
26426
3e43775ab21c getopt-wrapper.c: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
56 // If we don't have enough memory even to start Octave
3e43775ab21c getopt-wrapper.c: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
57 // then we might as well quit now.
3e43775ab21c getopt-wrapper.c: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
58 if (! retval)
3e43775ab21c getopt-wrapper.c: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
59 abort ();
3e43775ab21c getopt-wrapper.c: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
60
22064
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 p = opts;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 q = retval;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 while (p->name)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 {
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 q->name = p->name;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 switch (p->has_arg)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 {
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 case octave_required_arg:
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 q->has_arg = required_argument;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 break;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 case octave_optional_arg:
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 q->has_arg = optional_argument;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 break;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 default:
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 q->has_arg = no_argument;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 break;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 }
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 q->flag = p->flag;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 q->val = p->val;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 q++;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 p++;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 }
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 q->name = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 q->has_arg = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 q->flag = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 q->val = 0;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 return retval;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 }
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 int
22514
1e007e986396 Fix octave_getopt_long_wrapper prototype to avoid warnings on some systems (bug #49131)
Mike Miller <mtmiller@octave.org>
parents: 22402
diff changeset
99 octave_getopt_long_wrapper (int argc, char **argv,
22064
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 const char *shortopts,
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 const struct octave_getopt_options *longopts,
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 int *longind)
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 {
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 struct option *lopts = make_option_struct (longopts);
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 int retval = getopt_long (argc, argv, shortopts, lopts, longind);
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 free (lopts);
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 return retval;
bd4f3d2ce52f provide wrapper for getopt
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 }
22090
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
112
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
113 char *
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
114 octave_optarg_wrapper (void)
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
115 {
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
116 return optarg;
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
117 }
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
118
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
119 int
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
120 octave_optind_wrapper (void)
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
121 {
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
122 return optind;
66db9a493c84 avoid accessing optarg and optind directly (bug #48454)
John W. Eaton <jwe@octave.org>
parents: 22064
diff changeset
123 }
29910
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
124
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
125 int
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
126 octave_get_opterr_wrapper (void)
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
127 {
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
128 return opterr;
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
129 }
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
130
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
131 int
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
132 octave_set_opterr_wrapper (int val)
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
133 {
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
134 int retval = opterr;
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
135
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
136 opterr = val;
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
137
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
138 return retval;
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
139 }
0f41e117789f disable getopt error reporting in wrapper program (bug #60886)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
140