File: opt.cpp

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (283 lines) | stat: -rw-r--r-- 10,482 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012, 2013, 2015
 * Martin Lambers <marlam@marlam.de>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <typeinfo>
#include <limits>
#include <cstring>
#ifndef NDEBUG
# include <set>
#endif

#include <getopt.h>
#undef no_argument
#undef required_argument
#undef optional_argument

#include "base/dbg.h"
#include "base/msg.h"
#include "base/opt.h"

#include "base/gettext.h"
#define _(string) gettext(string)


namespace opt
{
    bool parse(int argc, char *argv[],
            std::vector<option *> &options,
            int min_arguments, int max_arguments,
            std::vector<std::string> &arguments)
    {
        const int optval_base = static_cast<int>(std::numeric_limits<unsigned char>::max()) + 1;
        int longopt_count;          /* Number of long options == number of options */
        int longopt_ind;            /* Index in options and longoptions */
        int shortopt_count;         /* Number of short options <= number of options */
        struct ::option *longopts;  /* long option description for getopt_long() */
        char *shortopts;            /* short option description for getopt_long() */
        int shortopts_ind;          /* index in short options */
        int *shortopt_opt_ind;      /* Mapping from index of short options to index of options */
        bool option_shortname;      /* Whether the short name of an option was used */
        bool *option_was_seen;      /* Whether option i was used */
        bool info_option_was_seen;  /* Whether an option of type opt_info was seen */
        bool error;

        info_option_was_seen = false;
        longopt_count = options.size();
        shortopt_count = 0;
        for (int i = 0; i < longopt_count; i++)
        {
            if (options[i]->shortname() != '\0')
            {
                shortopt_count++;
            }
        }
#ifndef NDEBUG
        /* Sanity check: are all short and long options unique? */
        {
            std::set<char> shortopt_set;
            std::set<std::string> longopt_set;
            for (size_t i = 0; i < options.size(); i++)
            {
                if (options[i]->shortname() != '\0')
                    shortopt_set.insert(options[i]->shortname());
                longopt_set.insert(options[i]->longname());
            }
            assert(longopt_set.size() == static_cast<size_t>(longopt_count));
            assert(shortopt_set.size() == static_cast<size_t>(shortopt_count));
        }
#endif
        /* Construct an array of type 'struct option', 'longopts', and a short
         * option description string 'shortopts' for use with getopt_long().
         * The indices of options in the given array 'options' and the constructed
         * array 'longopts' are the same. This is not true for the 'shortopts'
         * string. We use an index array 'shortopt_longopt_ind' in the following
         * way: If 'shortopts' is ":i::", then option i has index 1 in 'shortopts',
         * and 'options[shortopt_longopt_ind[1]]' is the corresponding option
         * description in 'options'.
         * The maximum length of 'shortopts' is 3*number_of_options+1: This is the
         * case when all options described in 'options' have corresponding short
         * options, and all have optional arguments. A leading colon in shortopts is
         * always needed. */
        longopts = new struct ::option[longopt_count + 1];
        shortopts = new char[1 + 3 * shortopt_count + 1];
        shortopts[0] = ':';
        shortopt_opt_ind = new int[1 + 3 * shortopt_count];
        shortopts_ind = 1;
        for (longopt_ind = 0; longopt_ind < longopt_count; longopt_ind++)
        {
            longopts[longopt_ind].name = options[longopt_ind]->longname().c_str();
            if (options[longopt_ind]->argument_policy() == option::no_argument)
            {
                longopts[longopt_ind].has_arg = 0;      // no_argument
            }
            else if (options[longopt_ind]->argument_policy() == option::optional_argument)
            {
                longopts[longopt_ind].has_arg = 2;      // optional_argument
            }
            else
            {
                longopts[longopt_ind].has_arg = 1;      // required_argument
            }
            longopts[longopt_ind].flag = NULL;
            longopts[longopt_ind].val = optval_base + longopt_ind;
            if (options[longopt_ind]->shortname() != '\0')
            {
                shortopts[shortopts_ind] = options[longopt_ind]->shortname();
                shortopt_opt_ind[shortopts_ind] = longopt_ind;
                shortopts_ind++;
                if (options[longopt_ind]->argument_policy() == option::no_argument)
                {
                }
                else if (options[longopt_ind]->argument_policy() == option::optional_argument)
                {
                    shortopts[shortopts_ind++] = ':';
                    shortopts[shortopts_ind++] = ':';
                }
                else
                {
                    shortopts[shortopts_ind++] = ':';
                }
            }
        }
        shortopts[shortopts_ind] = '\0';
        longopts[longopt_count].name = NULL;
        longopts[longopt_count].has_arg = 0;
        longopts[longopt_count].flag = 0;
        longopts[longopt_count].val = 0;

        /* The getopt_long() loop */
        option_was_seen = new bool[longopt_count];
        for (int i = 0; i < longopt_count; i++)
        {
            option_was_seen[i] = false;
        }
        error = false;
        opterr = 0;
        optind = 1;
#if defined HAVE_DECL_OPTRESET && HAVE_DECL_OPTRESET
        optreset = 1;
#endif
        while (!error)
        {
            int optval = getopt_long(argc, argv, shortopts, longopts, NULL);
            if (optval == -1)
            {
                break;
            }
            else if (optval == '?')
            {
                if (optopt == 0)
                {
                    msg::err(_("Invalid option %s."), argv[optind - 1]);
                }
                else if (optopt >= optval_base)
                {
                    msg::err(_("Option --%s does not take an argument."),
                            options[optopt - optval_base]->longname().c_str());
                }
                else
                {
                    msg::err(_("Invalid option -%c."), optopt);
                }
                error = true;
                break;
            }
            else if (optval == ':')
            {
                if (optopt >= optval_base)
                {
                    msg::err(_("Option --%s requires an argument."),
                            options[optopt - optval_base]->longname().c_str());
                }
                else
                {
                    msg::err(_("Option -%c requires an argument."), optopt);
                }
                error = true;
                break;
            }
            else if (optval < optval_base)
            {
                option_shortname = true;
                optval = shortopt_opt_ind[(strchr(shortopts, optval) - shortopts)];
            }
            else
            {
                option_shortname = false;
                optval -= optval_base;
            }
            /* options[optval] is the original description of the current option. */
            option_was_seen[optval] = true;
            if (!options[optval]->parse_argument(optarg ? optarg : std::string("")))
            {
                if (option_shortname)
                {
                    msg::err(_("Invalid argument for -%c."), options[optval]->shortname());
                }
                else
                {
                    msg::err(_("Invalid argument for --%s."), options[optval]->longname().c_str());
                }
                error = true;
            }
            else
            {
                options[optval]->mark_as_set();
            }
            if (typeid(*(options[optval])) == typeid(info))
            {
                info_option_was_seen = true;
            }
        }

        /* Test if all mandatory options were seen */
        if (!error && !info_option_was_seen)
        {
            for (int i = 0; i < longopt_count; i++)
            {
                if (options[i]->policy() == opt::required && !option_was_seen[i])
                {
                    if (options[i]->shortname() != '\0')
                    {
                        msg::err(_("Option --%s (-%c) is mandatory."), options[i]->longname().c_str(), options[i]->shortname());
                    }
                    else
                    {
                        msg::err(_("Option --%s is mandatory."), options[i]->longname().c_str());
                    }
                    error = true;
                    /* Do not break the loop. Print an error message for every missing option. */
                }
            }
        }

        /* Test if number of non-options arguments is ok */
        int args = argc - optind;
        if (!error && !info_option_was_seen)
        {
            if (args < min_arguments)
            {
                msg::err(_("Too few arguments."));
                error = true;
            }
            else if (max_arguments >= 0 && args > max_arguments)
            {
                msg::err(_("Too many arguments."));
                error = true;
            }
        }

        /* Return the arguments */
        if (!error)
        {
            arguments.clear();
            for (int i = 0; i < args; i++)
            {
                arguments.push_back(argv[optind + i]);
            }
        }

        delete[] longopts;
        delete[] shortopts;
        delete[] shortopt_opt_ind;
        delete[] option_was_seen;
        return !error;
    }
}