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