]>
Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - MI Option Parser. |
d01e8234 | 2 | Copyright (C) 2000-2025 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 | 19 | |
fb40c209 | 20 | #include "mi-getopt.h" |
242f1fd7 YQ |
21 | /* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h. |
22 | When there is an unknown option, if ERROR_ON_UNKNOWN is true, | |
23 | throw an error, otherwise return -1. */ | |
24 | ||
25 | static int | |
9158e49a TT |
26 | mi_getopt_1 (const char *prefix, int argc, const char *const *argv, |
27 | const struct mi_opt *opts, int *oind, const char **oarg, | |
242f1fd7 | 28 | int error_on_unknown) |
fb40c209 | 29 | { |
9158e49a | 30 | const char *arg; |
91174723 | 31 | const struct mi_opt *opt; |
102040f0 | 32 | |
2b03b41d | 33 | /* We assume that argv/argc are ok. */ |
324478ca | 34 | if (*oind > argc || *oind < 0) |
f34652de | 35 | internal_error (_("mi_getopt_long: oind out of bounds")); |
324478ca | 36 | if (*oind == argc) |
fb40c209 | 37 | return -1; |
324478ca | 38 | arg = argv[*oind]; |
fb40c209 AC |
39 | /* ``--''? */ |
40 | if (strcmp (arg, "--") == 0) | |
41 | { | |
324478ca AS |
42 | *oind += 1; |
43 | *oarg = NULL; | |
fb40c209 AC |
44 | return -1; |
45 | } | |
2b03b41d | 46 | /* End of option list. */ |
fb40c209 AC |
47 | if (arg[0] != '-') |
48 | { | |
324478ca | 49 | *oarg = NULL; |
fb40c209 AC |
50 | return -1; |
51 | } | |
2b03b41d | 52 | /* Look the option up. */ |
fb40c209 AC |
53 | for (opt = opts; opt->name != NULL; opt++) |
54 | { | |
55 | if (strcmp (opt->name, arg + 1) != 0) | |
56 | continue; | |
57 | if (opt->arg_p) | |
58 | { | |
2b03b41d | 59 | /* A non-simple oarg option. */ |
324478ca | 60 | if (argc < *oind + 2) |
8a3fe4f8 | 61 | error (_("%s: Option %s requires an argument"), prefix, arg); |
324478ca AS |
62 | *oarg = argv[(*oind) + 1]; |
63 | *oind = (*oind) + 2; | |
fb40c209 AC |
64 | return opt->index; |
65 | } | |
66 | else | |
67 | { | |
324478ca AS |
68 | *oarg = NULL; |
69 | *oind = (*oind) + 1; | |
fb40c209 AC |
70 | return opt->index; |
71 | } | |
72 | } | |
242f1fd7 YQ |
73 | |
74 | if (error_on_unknown) | |
75 | error (_("%s: Unknown option ``%s''"), prefix, arg + 1); | |
76 | else | |
77 | return -1; | |
78 | } | |
79 | ||
80 | int | |
81 | mi_getopt (const char *prefix, | |
9158e49a | 82 | int argc, const char *const *argv, |
242f1fd7 | 83 | const struct mi_opt *opts, |
9158e49a | 84 | int *oind, const char **oarg) |
242f1fd7 YQ |
85 | { |
86 | return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1); | |
87 | } | |
88 | ||
89 | int | |
9158e49a TT |
90 | mi_getopt_allow_unknown (const char *prefix, int argc, |
91 | const char *const *argv, | |
92 | const struct mi_opt *opts, int *oind, | |
93 | const char **oarg) | |
242f1fd7 YQ |
94 | { |
95 | return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0); | |
fb40c209 | 96 | } |
1abaf70c BR |
97 | |
98 | int | |
9158e49a | 99 | mi_valid_noargs (const char *prefix, int argc, const char *const *argv) |
1abaf70c | 100 | { |
324478ca | 101 | int oind = 0; |
9158e49a | 102 | const 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 | } |