Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - MI Option Parser. |
42a4f53d | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
ab91fdd5 | 3 | Contributed by Cygnus Solutions (a Red Hat company). |
fb40c209 AC |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
fb40c209 AC |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fb40c209 AC |
19 | |
20 | #include "defs.h" | |
21 | #include "mi-getopt.h" | |
242f1fd7 YQ |
22 | /* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h. |
23 | When there is an unknown option, if ERROR_ON_UNKNOWN is true, | |
24 | throw an error, otherwise return -1. */ | |
25 | ||
26 | static int | |
27 | mi_getopt_1 (const char *prefix, int argc, char **argv, | |
28 | const struct mi_opt *opts, int *oind, char **oarg, | |
29 | int error_on_unknown) | |
fb40c209 AC |
30 | { |
31 | char *arg; | |
91174723 | 32 | const struct mi_opt *opt; |
102040f0 | 33 | |
2b03b41d | 34 | /* We assume that argv/argc are ok. */ |
324478ca | 35 | if (*oind > argc || *oind < 0) |
8e65ff28 | 36 | internal_error (__FILE__, __LINE__, |
324478ca AS |
37 | _("mi_getopt_long: oind out of bounds")); |
38 | if (*oind == argc) | |
fb40c209 | 39 | return -1; |
324478ca | 40 | arg = argv[*oind]; |
fb40c209 AC |
41 | /* ``--''? */ |
42 | if (strcmp (arg, "--") == 0) | |
43 | { | |
324478ca AS |
44 | *oind += 1; |
45 | *oarg = NULL; | |
fb40c209 AC |
46 | return -1; |
47 | } | |
2b03b41d | 48 | /* End of option list. */ |
fb40c209 AC |
49 | if (arg[0] != '-') |
50 | { | |
324478ca | 51 | *oarg = NULL; |
fb40c209 AC |
52 | return -1; |
53 | } | |
2b03b41d | 54 | /* Look the option up. */ |
fb40c209 AC |
55 | for (opt = opts; opt->name != NULL; opt++) |
56 | { | |
57 | if (strcmp (opt->name, arg + 1) != 0) | |
58 | continue; | |
59 | if (opt->arg_p) | |
60 | { | |
2b03b41d | 61 | /* A non-simple oarg option. */ |
324478ca | 62 | if (argc < *oind + 2) |
8a3fe4f8 | 63 | error (_("%s: Option %s requires an argument"), prefix, arg); |
324478ca AS |
64 | *oarg = argv[(*oind) + 1]; |
65 | *oind = (*oind) + 2; | |
fb40c209 AC |
66 | return opt->index; |
67 | } | |
68 | else | |
69 | { | |
324478ca AS |
70 | *oarg = NULL; |
71 | *oind = (*oind) + 1; | |
fb40c209 AC |
72 | return opt->index; |
73 | } | |
74 | } | |
242f1fd7 YQ |
75 | |
76 | if (error_on_unknown) | |
77 | error (_("%s: Unknown option ``%s''"), prefix, arg + 1); | |
78 | else | |
79 | return -1; | |
80 | } | |
81 | ||
82 | int | |
83 | mi_getopt (const char *prefix, | |
84 | int argc, char **argv, | |
85 | const struct mi_opt *opts, | |
86 | int *oind, char **oarg) | |
87 | { | |
88 | return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1); | |
89 | } | |
90 | ||
91 | int | |
92 | mi_getopt_allow_unknown (const char *prefix, int argc, char **argv, | |
93 | const struct mi_opt *opts, int *oind, char **oarg) | |
94 | { | |
95 | return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0); | |
fb40c209 | 96 | } |
1abaf70c BR |
97 | |
98 | int | |
99 | mi_valid_noargs (const char *prefix, int argc, char **argv) | |
100 | { | |
324478ca AS |
101 | int oind = 0; |
102 | char *oarg; | |
91174723 | 103 | static const struct mi_opt opts[] = |
2b03b41d SS |
104 | { |
105 | { 0, 0, 0 } | |
106 | }; | |
1abaf70c | 107 | |
324478ca | 108 | if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1) |
1abaf70c BR |
109 | return 1; |
110 | else | |
111 | return 0; | |
112 | } |