Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - MI Option Parser. |
9b254dd1 | 2 | Copyright (C) 2000, 2001, 2007, 2008 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" | |
27b82ed2 | 22 | #include "gdb_string.h" |
fb40c209 AC |
23 | |
24 | int | |
25 | mi_getopt (const char *prefix, | |
26 | int argc, char **argv, | |
27 | struct mi_opt *opts, | |
28 | int *optind, char **optarg) | |
29 | { | |
30 | char *arg; | |
31 | struct mi_opt *opt; | |
32 | /* We assume that argv/argc are ok. */ | |
33 | if (*optind > argc || *optind < 0) | |
8e65ff28 | 34 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 35 | _("mi_getopt_long: optind out of bounds")); |
fb40c209 AC |
36 | if (*optind == argc) |
37 | return -1; | |
38 | arg = argv[*optind]; | |
39 | /* ``--''? */ | |
40 | if (strcmp (arg, "--") == 0) | |
41 | { | |
42 | *optind += 1; | |
43 | *optarg = NULL; | |
44 | return -1; | |
45 | } | |
46 | /* End of option list. */ | |
47 | if (arg[0] != '-') | |
48 | { | |
49 | *optarg = NULL; | |
50 | return -1; | |
51 | } | |
52 | /* Look the option up. */ | |
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 | { | |
59 | /* A non-simple optarg option. */ | |
60 | if (argc < *optind + 2) | |
8a3fe4f8 | 61 | error (_("%s: Option %s requires an argument"), prefix, arg); |
fb40c209 AC |
62 | *optarg = argv[(*optind) + 1]; |
63 | *optind = (*optind) + 2; | |
64 | return opt->index; | |
65 | } | |
66 | else | |
67 | { | |
68 | *optarg = NULL; | |
69 | *optind = (*optind) + 1; | |
70 | return opt->index; | |
71 | } | |
72 | } | |
8a3fe4f8 | 73 | error (_("%s: Unknown option ``%s''"), prefix, arg + 1); |
fb40c209 | 74 | } |
1abaf70c BR |
75 | |
76 | int | |
77 | mi_valid_noargs (const char *prefix, int argc, char **argv) | |
78 | { | |
79 | int optind = 0; | |
80 | char *optarg; | |
81 | static struct mi_opt opts[] = | |
82 | { | |
d5d6fca5 | 83 | { 0, 0, 0 } |
1abaf70c BR |
84 | }; |
85 | ||
86 | if (mi_getopt (prefix, argc, argv, opts, &optind, &optarg) == -1) | |
87 | return 1; | |
88 | else | |
89 | return 0; | |
90 | } |